【问题标题】:How to set diffrent images for every cell in Objective-C?如何为 Objective-C 中的每个单元格设置不同的图像?
【发布时间】:2013-11-07 19:45:32
【问题描述】:

我有 TableView,我想为每个单元格设置不同的图像。 现在我看到每个单元格的图像都相同

这是我的代码:

@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

【问题讨论】:

    标签: objective-c tableview cells uitableview


    【解决方案1】:

    您需要根据单元格的 indexPath 获取图像名称。例如,更新您的 tableData 以包含图像名称。然后你得到图像名称就像你得到单元格的文本一样。这可以通过将tableData 更改为一个字典数组来实现,其中每个字典都有文本和图像名称。

    【讨论】:

    • 是吗?UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    • 没有。创建单元格的线不会改变。您设置图像的行需要更改。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    • 2013-08-04
    • 1970-01-01
    • 2017-04-13
    相关资源
    最近更新 更多