gpt4 book ai didi

ios - 在 UITableView 上显示图像的正确方式?

转载 作者:行者123 更新时间:2023-11-28 20:18:22 25 4
gpt4 key购买 nike

我正在开发一个应用程序,它从 3 个不同的共享日历中获取事件,并在 UITableView 中按日期排序显示它们。第一个日历是“电视上播放的体育节目”第二个日历是“舞台上的现场音乐”第三个日历用于固定事件(例如每个星期四是“女士之夜”)

我想在单元格上显示一个小图像,图像需要根据事件发生变化。我现在的做法是在事件描述中写入图像文件名,然后使用它来设置图像。

cell.imageView.image = [UIImage imageNamed:[entry valueForKeyPath:@"content.$t"]];

我想做的是根据日历名称或事件标题的内容设置图像

我试过了

NSArray *gdWho = entry[@"gd$who"];
NSString *calendar = gdWho[0][@"valueString"];
if([calendar isEqualToString:@"Live Music on Stage"])
{
cell.imageView.image = [UIImage imageNamed:@"music-notes-40x40.jpg"];
}

它有效,但只要我滚动表格,它们就会混淆。我在某处读到这是预期结果,因为滚动时单元格索引路径会发生变化,从而使我的代码无法使用。

但是你会怎么做呢。谁能给我指出正确的方向——我真的很想把艺术家在舞台上现场演奏的图像放上去,所以我的最佳解决方案是在 NSString = [entry valueForKeyPath:@"时显示图像title.$t"] 包含例如名称“dennis”

最佳答案

it works, but as soon as i scroll the tables they get mixed up

这表明 UITableViewCell“回收”存在问题。您需要更改 tableview:cellforrowatindexpath: 的代码,使其无条件地设置 cell.imageView.image,而不仅仅是在第一次创建单元格时时间。

此外,您可以用在字典中查找来替换检查 calendar 各种特定值的 if 语句链,如下所示:

// This code goes into your viewDidLoad method. imageForCalendarType is an ivar
imageForCalendarType = @{
@"Live Music on Stage" : [UIImage imageNamed:@"music-notes-40x40.jpg"]
, @"Sports Shown on TV" : [UIImage imageNamed:@"sport-tv-40x40.jpg"]
, @"Fixed Event" : [UIImage imageNamed:@"fixed-40x40.jpg"]
};

现在您可以使用字典中的图像,如下所示:

// This goes into your tableview:cellforrowatindexpath: method
cell.imageView.image = imageForCalendarType[calendar];

关于ios - 在 UITableView 上显示图像的正确方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16945228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com