我的应用由 3 个视图组成(视图 1 (Tableview1)、视图 2 (Tableview2)、视图 3 (DetailView))。
我在 View 2 (Tableview2) 中设置了一个自定义 tableview 单元格,其中包含子视图 (imageViews 和 Labels)。
这些子视图之一是 ImageView,如果单元格 ID 在收藏夹数组中,则包含图像。当您选择一个 cellForRowAtIndexpath 时,您将被推送到一个新视图(视图 3 (DetailView)),您可以在其中按下一个按钮并使该项目成为收藏夹。当您关闭该视图并返回时,会显示图像,并且一切正常。
当我尝试取消选择收藏项时出现我的问题。最喜欢的符号仍然显示在 tableView 中,直到我返回 View 1 (Tableview1)。
我尝试在 ViewWillAppear 的视图 2 中调用 [myTableview reloadData],但收藏夹符号仍然存在。我的收藏夹数组已保存并加载了正确的值。
数组在 ViewWillAppear 处更新...
可能是什么问题呢?
相关代码:编辑 - 从 cellForRowAtIndexPath 发布所有相关代码:一切都显示正确,但如果我从自定义单元格中删除图像,tableview 不会更新... 编辑:使用 for 循环中的 else 语句更新以尝试删除不再在收藏夹数组中的项目中的收藏图像。编辑 编辑 编辑:现在工作,在我的 for 循环中添加了一个 break 语句......更新了代码,以便它可以帮助其他人......
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = (CustomCell *)[myTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
[cell setAccessoryType: UITableViewCellAccessoryDisclosureIndicator];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *kategori = [prefs objectForKey:@"kategoriID"];
NSLog(@"KategoriID = %@", kategori);
rowArray = nil;
rowArray = [[NSMutableArray alloc] init];
for(int i=0; i < [listOfItems count]; i++) {
if([kategori isEqualToString:[listOfItems objectAtIndex:i]])
{
//Tilføjer ID
[rowArray addObject:[NSNumber numberWithInt:i-5]];
}
}
int arrayValue = [[rowArray objectAtIndex:indexPath.row] intValue];
cell.primaryLabel.text = [listOfItems objectAtIndex:arrayValue+1];
cell.secondaryLabel.text = [listOfItems objectAtIndex:arrayValue+2];
cell.myImageView.image = [UIImage imageNamed:[listOfItems objectAtIndex:arrayValue+4]];
//Time
cell.timeImageView.image = [UIImage imageNamed:@"03-clock.png"];
cell.timeLabel.text = [listOfItems objectAtIndex:arrayValue+6];
//Views
cell.viewsImageView.image = [UIImage imageNamed:@"04-eye.png"];
cell.viewsLabel.text = [listOfItems objectAtIndex:arrayValue+7];
//Stars
NSString *starsString = [listOfItems objectAtIndex:arrayValue+8];
if ([starsString isEqualToString:@"S0"]) {
cell.starsImageView.image = [UIImage imageNamed:@"0-stars.png"];
}
if ([starsString isEqualToString:@"S0_1"]) {
cell.starsImageView.image = [UIImage imageNamed:@"0_1-stars.png"];
}
if ([starsString isEqualToString:@"S1"]) {
cell.starsImageView.image = [UIImage imageNamed:@"1-stars.png"];
}
if ([starsString isEqualToString:@"S1_1"]) {
cell.starsImageView.image = [UIImage imageNamed:@"1_1-stars.png"];
}
if ([starsString isEqualToString:@"S2"]) {
cell.starsImageView.image = [UIImage imageNamed:@"2-stars.png"];
}
if ([starsString isEqualToString:@"S2_1"]) {
cell.starsImageView.image = [UIImage imageNamed:@"2_1-stars.png"];
}
if ([starsString isEqualToString:@"S3"]) {
cell.starsImageView.image = [UIImage imageNamed:@"3-stars.png"];
}
if ([starsString isEqualToString:@"S3_1"]) {
cell.starsImageView.image = [UIImage imageNamed:@"3_1-stars.png"];
}
if ([starsString isEqualToString:@"S4"]) {
cell.starsImageView.image = [UIImage imageNamed:@"4-stars.png"];
}
if ([starsString isEqualToString:@"S4_1"]) {
cell.starsImageView.image = [UIImage imageNamed:@"4_1-stars.png"];
}
if ([starsString isEqualToString:@"S5"]) {
cell.starsImageView.image = [UIImage imageNamed:@"5-stars.png"];
}
//favorite
NSString *IDString = [listOfItems objectAtIndex:arrayValue];
NSLog(@"ID String: %@", IDString);
if (videoerIFavoritDatabase == 0) {
NSLog(@"Der er ingen videoer i favorit databasen!");
}
else {
for(int i=0; i < [tableviewFavoritArray count]; i++) {
NSLog(@"ID Array Value:%i", i);
if([IDString isEqualToString:[tableviewFavoritArray objectAtIndex:i]])
{
cell.favoriteImageView.image = [UIImage imageNamed:@"favoriteTableview@2x.png"];
NSLog(@"ER favorit:%i = ID%@", i, [tableviewFavoritArray objectAtIndex:i]);
break;
}
else { cell.favoriteImageView.image = [UIImage imageNamed:@"empty.png"];
break;
}
}
}
return cell;
}