如何为Objective-C中的每个细胞设置不同的图像?
我有TableView,我想为每个单元格设置不同的图像。 现在我看到每个细胞都有相同的图像 这是我的密码:如何为Objective-C中的每个细胞设置不同的图像?,objective-c,tableview,cells,uitableview,Objective C,Tableview,Cells,Uitableview,我有TableView,我想为每个单元格设置不同的图像。 现在我看到每个细胞都有相同的图像 这是我的密码: @implementation SecondViewController { NSArray *tableData; } - (void)viewDidLoad { [super viewDidLoad]; tableData = [NSArray arrayWithObjects:@"Restore Edilen Evler", @"Sakarya Kenarı"
@implementation SecondViewController
{
NSArray *tableData;
}
- (void)viewDidLoad
{
[super viewDidLoad];
tableData = [NSArray arrayWithObjects:@"Restore Edilen Evler", @"Sakarya Kenarı", @"Tekke Seyir Tepesi", @"Kilise",@"Akkaya Şelalesi",@"Kaplıcalar",nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
cell.textLabel.numberOfLines = 2;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"aktivite4.png"];
return cell;
}
@end
您需要根据单元格的索引XPath获取图像名称。例如,更新
tableData
以包含图像名称。然后得到图像名称,就像得到单元格文本一样。这可以通过将tableData
更改为字典数组来实现,其中每个字典都有文本和图像名称。是吗?UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];不。创建单元格的行不会更改。设置图像的行需要更改。