详解iOS App中UITableView的创建与内容刷新

2020-01-15 14:09:21王冬梅

创建并设置每行显示的内容


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
 static NSString *CellWithIdentifier = @"Cell"; 
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier]; 
 if (cell == nil) { 
  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellWithIdentifier]; 
 } 
 NSUInteger row = [indexPath row]; 
 cell.textLabel.text = [self.dataList objectAtIndex:row]; 
 cell.imageView.image = [UIImage imageNamed:@"green.png"]; 
 cell.detailTextLabel.text = @"详细信息"; 
 return cell; 
} 

UITableViewCell的样式也是可以进行设置的,如果不能满足项目的需要,可以自己定义UITableViewCell的样式
UITableViewCellStyleDefault

iOS,App,UITableView

UITableViewCellStyleSubtitle

iOS,App,UITableView

UITableViewCellStyleValue1

iOS,App,UITableView

UITableViewCellStyleValue2

iOS,App,UITableView

分组的TableView还可以进行内容的分段,是通过下面的方法实现,返回的数字1代表分为1段


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
 return 1; 
} 

设置内容缩进


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