UITableView 表视图

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//此方法每一行创建的时候都要调用一次
static NSString * sectionID=@"sectionID_1";//sectionID 当前只有一个section
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:sectionID];

if (cell==nil)
{
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionID];
}
cell.imageView.image=[UIImage imageNamed:@"title.png"];
cell.imageView.highlightedImage=[UIImage imageNamed:@"converse.png"];
NSInteger row=[indexPath row];//获取当前行
cell.textLabel.text=[titleList objectAtIndex:row];//为当前行赋值
return cell;
}



1.修改显示的文本

cell.textLabel.text=[titleList objectAtIndex:row];


2.修改显示在每行左侧的图片

    cell.imageView.image=[UIImage imageNamed:@"title.png"];
cell.imageView.highlightedImage=[UIImage imageNamed:@"converse.png"];

3.显示详细文本

需要将TableViewCell的style从UITableViewCellStyleDefault改成UITableViewCellStyleSubtitle或别的

不同style显示方式不同

cell.detailTextLabel.text=@"hahhahaha";

4.设置缩进级别

实现UITableViewDataSource协议中的

-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath

方法

-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [indexPath row];
}

 

5.对选中行的处理

禁止某行被选中

-(NSIndexPath *) tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row=[indexPath row];
if (row==0)
return nil;
return indexPath;
}


选中行弹出alert

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row=[indexPath row];
NSString *rowstring=[titleList objectAtIndex:row];
NSString * message=[NSString stringWithFormat:@"you selected %@",rowstring];
UIAlertView * alert1=[[UIAlertView alloc] initWithTitle:@"row selected" message:message delegate:self cancelButtonTitle:@"确定"otherButtonTitles:nil];
[alert1 show];
[tableview1 deselectRowAtIndexPath:indexPath animated:YES];
}

 

6.修改字体大小

 cell.textLabel.font=[UIFont boldSystemFontOfSize:50];

 

字体太大超过了行高,实现修改行高的方法:

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 70;
}



7.增加、删除cell:

    NSArray *groupindexpath=[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]];//插入的位置,必须和数据源一致。因为插入的数据是来自数据源中的相同下标位置。
    [titleList insertObject:@"hello" atIndex:0];//数据源中相同位置插入
[tableview1 insertRowsAtIndexPaths:groupindexpath withRowAnimation:UITableViewRowAnimationTop];
[tableview1 endUpdates];//刷新


 http://blog.sina.com.cn/s/blog_67419c420100ruej.html iphone开发-----关于tableview中row或者section的删除,添加

原文地址:https://www.cnblogs.com/phoenix13suns/p/2342799.html