代码之家  ›  专栏  ›  技术社区  ›  Pugalmuni Tom Tharakan

如何在iPhone的表视图单元中设置背景图像?

  •  2
  • Pugalmuni Tom Tharakan  · 技术社区  · 15 年前

    我要设置单元格的背景图像。我已经为表格视图中的单元格设置了背景图像。但我想为“表视图”单元格中的背景图像设置两个图像。我想为背景设置备用单元格,

    像,

      Image1- Cell 1, Cell 3, Cell 5, Etc.,
    
      Imgae-2- Cell 2, Cell 4,Cell 6, Etc.,
    

    请给我一些链接示例。

    谢谢!

    1 回复  |  直到 14 年前
        1
  •  8
  •   epatel    15 年前

    您可以为以下均匀/不均匀行设置两个不同的单元格

    - (UITableViewCell *)tableView:(UITableView *)tableView 
             cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {    
        static NSString *CellIdentifierEvenRows = @"MyCellEven";
        static NSString *CellIdentifierUnevenRows = @"MyCellUneven";
        UITableViewCell *cell;
    
        if (indexPath.row % 2) {
            cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierUnevenRows];
            if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                               reuseIdentifier:CellIdentifierUnevenRows] autorelease];
                cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"uneven.png"] autorelease];
            }
        } else {
            cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierEvenRows];
            if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                               reuseIdentifier:CellIdentifierEvenRows] autorelease];
                cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"even.png"] autorelease];
            }
        }
    
        return cell;
    }
    

    (尽管没有测试)

    推荐文章