2011-10-28 31 views
1

的部分我有我的UITableView具体的UITableViewCell在UITableView的

if (indexPath.row == 6){ 
     UIImageView *blog = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"blog.png"]]; 
     [cell setBackgroundView:blog]; 
     UIImageView *selectedblog = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"blogSel.png"]]; 
     cell.selectedBackgroundView=selectedblog; 
     cell.backgroundColor = [UIColor clearColor]; 
     [[cell textLabel] setTextColor:[UIColor whiteColor]]; 
     return cell;} 

各指标的代码,我有两个部分,在每节5行。如何将第1节中的indexPath.row 1到5以及第2节中的indexPath.row 6到10?

回答

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 5; 
} 

现在,您的表格视图将预期2个部分各有5行,并尝试绘制它们。然后,在cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSUInteger actualIndex = indexPath.row; 
    for(int i = 1; i < indexPath.section; ++i) 
    { 
     actualIndex += [self tableView:tableView 
           numberOfRowsInSection:i]; 
    } 

    // you can use the below switch statement to return 
    // different styled cells depending on the section 
    switch(indexPath.section) 
    { 
     case 1: // prepare and return cell as normal 
     default: 
      break; 

     case 2: // return alternative cell type 
      break; 
    } 
} 

actualIndex上述逻辑导致:

  • 第1节,1行至X返回indexPath.row不变
  • 第2节,排数1至Y返回X + indexPath.row
  • 第3章,行1到Z返回X + Y + indexPath.row
  • 可扩展到任何数量的部分

如果您有一个支持表格单元格的项目的底层数组(或其他平坦的容器类),这将允许您使用这些项目在表格视图中填写多个部分。

相关问题