IOS UITableView中行的操纵

此次就不从头建树工程了,在http://www.oschina.net/code/snippet_164134_9876下载工程。这个工程就是最简单的产生一个表格并向此中写入数据。用Xcode 4.2打开它,在这个工程根蒂根基上实现以上操纵。

1、标识表记标帜行

这里讲的标识表记标帜行指的是单击此行,可以实如今此行右边呈现一个勾,如下图所示:

为了实现标识表记标帜功能,在ViewController.m中@end之前添加代码:

#pragma mark -
#pragma mark Table Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *oneCell = [tableView cellForRowAtIndexPath: indexPath];
    if (oneCell.accessoryType == UITableViewCellAccessoryNone) {
        oneCell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else 
        oneCell.accessoryType = UITableViewCellAccessoryNone;
    [tableView deRowAtIndexPath:indexPath animated:YES]; 
}

该代码实现:单击某行时,若此行未被标识表记标帜,则标识表记标帜此行;若此行已经被标识表记标帜,则作废标识表记标帜。

运行结果如上图。

上方的代码实际上就是批改某行的accessoryType属性,这个属性可以设为四个常量:

UITableViewCellAccessoryCheckmark
UITableViewCellAccessoryDetailDisclosureButton
UITableViewCellAccessoryDisclosureIndicator
UITableViewCellAccessoryNone

结果依次如下图所示:

????? ???? ?

?? UITableViewCellAccessoryCheckmark??????? ??? UITableViewCellAccessoryDetailDisclosureButton

?????????????? ?

UITableViewCellAccessoryDisclosureIndicator??????????????? ? ?UITableViewCellAccessoryNone

重视,上方第二张图片中的蓝色圆圈不仅仅是一个图标,还是一个控件,点击它可以触发事务,在上一篇博客《iOS开辟16:应用Navigation Controller切换视图》应用过。

2、移动行

想要实现移动或者删除行如许的操纵,须要启动表格的编辑模式。应用的是setEditing:animated:办法。

2.1 打开ViewController.xib,将此中的表格控件映射成Outlet到ViewController.h,名称为myTableView。

2.2 打开ViewController.m,在viewDidLoad办法最后添加代码:

//启动表格的编辑模式
[self.myTableView setEditing:YES animated:YES];

2.3 在@end之前添加代码:

//打开编辑模式后,默认景象下每行左边会呈现红的删除按钮,这个办法就是封闭这些按钮的
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return UITableViewCellEditingStyleNone; 
} 

//这个办法用来告诉表格 这一行是否可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { 
    return YES; 
}

//这个办法就是履行移动操纵的
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)
        sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    NSUInteger Row = [sourceIndexPath row]; 
    NSUInteger toRow = [destinationIndexPath row]; 
    
    id object = [list objectAtIndex:Row]; 
    [list removeObjectAtIndex:Row]; 
    [list Object:object atIndex:toRow]; 
}

editingStyleForRowAtIndexPath这个办法顶用到了常量UITableViewCellEditingStyleNone,它默示不成编辑,这里的编辑指的是删除和插入。默示表格行的编辑模式的常量有:

UITableViewCellEditingStyleDelete
UITableViewCellEditingStyleInsert
UITableViewCellEditingStyleNone

顾名思义,第一个默示删除,第二个默示插入,第三个默示不成编辑。

若将editingStyleForRowAtIndexPath办法中的UITableViewCellEditingStyleNone依次换成上方三个值,则它们运行的结果依次如下图所示:

????

2.4 运行,从下图可以看到实现了行的移动:

然则也会发明,如今无法对每行进行标识表记标帜了。这申明,在编辑模式下,无法选择行,从而didSelectRowAtIndexPath这个办法不会履行。

3、删除行

从第2步过来,实现删除某行,其实斗劲简单了。

3.1将editingStyleForRowAtIndexPath办法中的UITableViewCellEditingStyleNone批改成UITableViewCellEditingStyleDelete。

3.2 在@end之前添加代码:

//这个办法按照参数editingStyle是UITableViewCellEditingStyleDelete
//还是UITableViewCellEditingStyleDelete履行删除或者插入
- (void)tableView:(UITableView *)tableView commitEditingStyle:
    (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger row = [indexPath row];
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.list removeObjectAtIndex:row]; 
        [tableView RowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                         withRowAnimation:UITableViewRowAnimationAutomatic]; 
    }
}

?

在这个办法中又呈现了一个常量:UITableViewRowAnimationAutomatic,它默示删除时的结果,类似的常量还有:

UITableViewRowAnimationAutomatic
UITableViewRowAnimationTop
UITableViewRowAnimationBottom
UITableViewRowAnimationLeft
UITableViewRowAnimationRight
UITableViewRowAnimationMiddle
UITableViewRowAnimationFade
UITableViewRowAnimationNone

它们的结果就不一一介绍了,可以在实际应用时尝尝。

3.3 运行,看看结果:

????

刚运行时显示如左边的图片,点击某一行左边的圆圈图标,会显示如中心图片所示。然后点击Delegate按钮,那一行就会被删除掉,如右边的那张图片所示,它显示的是删除时的结果。

4、插入行

这个与删除行类似。

4.1 起首将editingStyleForRowAtIndexPath办法中的UITableViewCellEditingStyleDelete批改成UITableViewCellEditingStyleInsert。

4.2在3.2添加的办法中添加代码:

else {
    //我们实现的是在所选行的地位插入一行,是以直接应用了参数indexPath
    NSArray *IndexPaths = [NSArray arrayWithObjects:indexPath,nil];
    //同样,将数据加到list中,用的row
    [self.list Object:@"新添加的行" atIndex:row];
    [tableView RowsAtIndexPaths:IndexPaths withRowAnimation:UITableViewRowAnimationRight];
}

上方的代码中也可以不消RowsAtIndexPaths办法,而直接应用[tableView reloadData];语句,然则如许就没有添加的结果了。

4.3 好了,运行一下:

????

刚运行时如上方左图所示,单击了某个加号后,新的一行就从右边飞进来了,因为在RowsAtIndexPaths顶用了参数UITableViewRowAnimationRight。

?

UITableView每个cell之间的默认分别线如何去掉

很简单,只须要

tableView.separatorStyle?= NO;


?

UITableView

-、建树 UITableView

?DataTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 420)];
?[DataTable setDelegate:self];
?[DataTable setDataSource:self];
?[self.view addSubview:DataTable];
?[DataTable release];


二、UITableView各Method申明

?


//Section总数
- (NSArray *)sectionIndexTitlesForTableView:(UITableView*)tableView{
?return TitleData;
}


// Section Titles
//每个section显示的题目
- (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section{
?return @"";
}


//指定有几许个分区(Section),默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
?return 4;
}


//指定每个分区中有几许行,默认为1
- (NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section{
}


//绘制Cell
-(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier =@"SimpleTableIdentifier";
??
???UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:
????????????????????????????SimpleTableIdentifier];
????if (cell ==nil) {??
???????cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
??????????????????????????????????????reuseIdentifier: SimpleTableIdentifier] autorelease];
?}
?cell.imageView.image=image;//未选cell时的图片
?cell.imageView.highlightedImage=highlightImage;//选中cell后的图片
?cell.text=//.....
?return cell;
}


//行缩进
-(NSInteger)tableView:(UITableView *)tableViewindentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
?NSUInteger row = [indexPath row];
?return row;
}


//改变行的高度
- (CGFloat)tableView:(UITableView *)tableViewheightForRowAtIndexPath:(NSIndexPath *)indexPath{
????return40;
}


//定位
[TopicsTable setContentOffset:CGPointMake(0, promiseNum * 44 +Chapter * 20)];


//返回当前所选cell
NSIndexPath *ip = [NSIndexPath indexPathForRow:rowinSection:section];
[TopicsTable RowAtIndexPath:ip animated:YESscrollPosition:UITableViewScrollPositionNone];


[tableViewsetSeparatorStyle:UITableViewCellSelectionStyleNone];


//选中Cell响应事务
- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath{

?[tableView deRowAtIndexPath:indexPathanimated:YES];//选中后的反显色彩即刻消散
}


//断定选中的行(阻拦选中第一行)
-(NSIndexPath *)tableView:(UITableView *)tableViewwillSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
????NSUIntegerrow = [indexPath row];
????if (row ==0)
???????return nil;
???
????returnindexPath;
}


//划动cell是否呈现del按钮
- (BOOL)tableView:(UITableView *)tableViewcanEditRowAtIndexPath:(NSIndexPath *)indexPath {
}


//编辑状况
- (void)tableView:(UITableView *)tableViewcommitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{


[topicsTable setContentSize:CGSizeMake(0,controller.promiseNum *44)];
//右侧添加一个索引表
- (NSArray *)sectionIndexTitlesForTableView:(UITableView*)tableView{
}

//返回Section题目内容
- (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section{
}

//自定义划动时del按钮内容
- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexPath


//跳到指的row or section
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0inSection:0] atScrollPosition:UITableViewScrollPositionBottomanimated:NO];

?

三、在UITableViewCell上建树UILable多行显示

- (UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {
????staticNSString *CellIdentifier =@"Cell";???
???UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];
????if (cell ==nil) {
???????cell = [[[UITableViewCell alloc] initWithFrame:CGRectZeroreuseIdentifier:CellIdentifier] autorelease];
??UILabel *Datalabel = [[UILabelalloc] initWithFrame:CGRectMake(10, 0, 320, 44)];
??[Datalabel setTag:100];
??Datalabel.autoresizingMask =UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;
??[cell.contentViewaddSubview:Datalabel];
??[Datalabel release];
?}?
?UILabel *Datalabel = (UILabel *)[cell.contentViewviewWithTag:100];
?[Datalabel setFont:[UIFontboldSystemFontOfSize:18]];
?Datalabel.text = [data.DataArrayobjectAtIndex:indexPath.row];
?cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
????returncell;
}

?

?

//选中cell时的色彩

typedef enum {
???UITableViewCellSelectionStyleNone,
???UITableViewCellSelectionStyleBlue,
???UITableViewCellSelectionStyleGray
} UITableViewCellSelectionStyle

?

//cell右边按钮格局

typedef enum {
?
??UITableViewCellAccessoryNone,??????????????????// don""t show any accessory view
???UITableViewCellAccessoryDisclosureIndicator,???// regular chevron. doesn""t track
???UITableViewCellAccessoryDetailDisclosureButton, // blue button w/chevron. tracks
???UITableViewCellAccessoryCheckmark??????????????// checkmark. doesn""t track
} UITableViewCellAccessoryType

?

//是否加换行线

typedef enum {
???UITableViewCellSeparatorStyleNone,
???UITableViewCellSeparatorStyleSingleLine
} UITableViewCellSeparatorStyle

?

//改变换行线色彩

tableView.separatorColor?=?[UIColor?blueColor];?斯蒂文生

你可能感兴趣的:(IOS UITableView中行的操纵)