【发布时间】:2009-08-19 10:13:12
【问题描述】:
我正在为 iPhone 制作一个应用程序,其中我正在创建包含 2 个部分的 UITableView。我已经创建了 UITableView 的部分,但问题是我需要在每个部分的每个单元格中使用不同的图像。有没有人对此有任何解决方案
【问题讨论】:
标签: objective-c iphone xcode uitableview
我正在为 iPhone 制作一个应用程序,其中我正在创建包含 2 个部分的 UITableView。我已经创建了 UITableView 的部分,但问题是我需要在每个部分的每个单元格中使用不同的图像。有没有人对此有任何解决方案
【问题讨论】:
标签: objective-c iphone xcode uitableview
是的,这很容易。你的数据源中有这个吗?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
添加以下代码:
NSString *CellIdentifier = [NSString stringWithFormat:@"cell_%i",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if (indexPath.section == 0 && indexPath.row == 0){
[cell.imageView setImage:[UIImage imageNamed:@"justanimage.png"]];
}else if (indexPath.section == 0 && indexPath.row == 1){
[cell.imageView setImage:[UIImage imageNamed:@"justanimage1.png"]];
}else if (indexPath.section == 1 && indexPath.row == 0){
[cell.imageView setImage:[UIImage imageNamed:@"justanimage2.png"]];
}else if (indexPath.section == 1 && indexPath.row == 1){
[cell.imageView setImage:[UIImage imageNamed:@"justanimage3.png"]];
}
}
您也可以将数据保存在 NSMutableArray 中,但这有点复杂。
【讨论】: