簡體   English   中英

uitableview部分中的特定uitableviewcell

[英]Specific uitableviewcell's in section of uitableview

我的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中?

- (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
  • 可擴展到任意數量的部分

如果您有支持表格單元格的基礎數組(或其他平面容器類),則可以使用這些項目在表格視圖中填寫多個部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM