如何为表格中的每个按钮添加图像

时间:2013-05-12 21:56:01

标签: iphone ios uitableview

如何为表格中的每个按钮添加图像

表格中有8个按钮,我想为每个按钮添加不同的图像

我有这个如何修改代码以接受8个图像

- (UIImage *)cellBackgroundForRowAtIndexPath:(NSIndexPath *)indexPath
{  
NSInteger rowCount = [self tableView:[self tableView] numberOfRowsInSection:0];
NSInteger rowIndex = indexPath.row;
UIImage *background = nil;

if (rowIndex == 0) {
    background = [UIImage imageNamed:@"Button01.png"];
} else if (rowIndex == rowCount - 1) {
    background = [UIImage imageNamed:@"Button02.png"];
} else {
    background = [UIImage imageNamed:@"Button03.png"];
}

return background;
}

1 个答案:

答案 0 :(得分:0)

形成一个包含图像所有图像名称的数组。

self.imagesArray = @[@"Button01.png",@"Button02.png",@"Button03.png",...];

然后在tableView:cellForRowAtIndexPath:内或使用indexPath配置单元格的位置访问它,以便从imagesArray

中查找相应的图像
- (UIImage *)cellBackgroundForRowAtIndexPath:(NSIndexPath *)indexPath
{  
    NSArray *imagesArray = @[@"Button01.png",@"Button02.png",@"Button03.png"];

    return [UIImage imageNamed:imagesArray[indexPath.row];
}