自定义 Tableview 单元格出现问题
我的应用程序由 3 个视图组成(视图 1 (Tableview1)、视图 2 (Tableview2)、视图 3 (DetailView))。
我在视图 2 (Tableview2) 中设置了一个带有子视图(imageViews 和标签)的自定义表格视图单元格。
这些子视图之一是 ImageView,如果单元格 ID 位于收藏夹数组中,则包含图像。当您选择 cellForRowAtIndexpath 时,您将被推送到新视图(视图 3 (DetailView)),您可以在其中按下按钮并将该项目设为收藏夹。 当您关闭该视图并返回时,将显示图像,并且一切正常。
当我尝试取消选择收藏的项目时,就会出现问题。最喜欢的符号仍然显示在 tableView 中,直到我返回到 View 1 (Tableview1)。
我尝试在 ViewWillAppear 的视图 2 中调用 [myTableview reloadData],但收藏夹符号仍然存在。我的最爱数组已保存并加载了正确的值。
数组在 ViewWillAppear 处更新...
可能是什么问题?
相关代码:编辑 - 发布了 cellForRowAtIndexPath 中的所有相关代码:一切都显示正确,但如果我从自定义单元格中删除图像,则表视图不会更新...编辑:使用 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:@"[email protected]"];
NSLog(@"ER favorit:%i = ID%@", i, [tableviewFavoritArray objectAtIndex:i]);
break;
}
else { cell.favoriteImageView.image = [UIImage imageNamed:@"empty.png"];
break;
}
}
}
return cell;
}
My App consists of 3 views (View 1 (Tableview1), View 2 (Tableview2), View 3 (DetailView)).
I have a custom tableview cell set up with subviews (imageViews and Labels) in my View 2 (Tableview2).
One of these subviews is an ImageView, and contains an image if the cell ID is in a favorites Array. When you select a cellForRowAtIndexpath you are pushed to a new view (View 3 (DetailView)), where you can press a button and make the item a favorite.
When you close that view and go back the image is shown, and everything works fine.
My problem occurs when I try to deselect an item as favorite. The favorite symbol is still shown in the tableView, until i go back to View 1 (Tableview1).
I have tried to call [myTableview reloadData], in View 2 on ViewWillAppear, but the favorites symbol is still there. My favorites Array is saved and loaded with the correct values.
The Array gets updated at ViewWillAppear...
What could be the problem?
Relevant code: EDIT - Posted all relevant code from cellForRowAtIndexPath: Everything is showing right, but the tableview does not get updated if I remove an image from the custom cell... EDIT: Updated with else statement in the for loop to try and remove the favorite images from the items that are no longer in the favorites array.
EDIT EDIT EDIT: Working now, added a break statement in my for loop... Updated code so that it might help others...
- (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:@"[email protected]"];
NSLog(@"ER favorit:%i = ID%@", i, [tableviewFavoritArray objectAtIndex:i]);
break;
}
else { cell.favoriteImageView.image = [UIImage imageNamed:@"empty.png"];
break;
}
}
}
return cell;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在
if (cell == nil)
语句中完成所有单元配置。仅当 tableView 未从出队方法返回单元格时才会执行此代码。可能发生的情况是您正在重复使用单元格并且它们没有被配置,因此数据和外观保持不变。您的 cellForRowAtIndexPath 方法应具有以下结构:
根据你的情况更新的代码,问题似乎是您正在设置最喜欢的图像,但如果单元格不再是最喜欢的,则不会取消设置您需要一个<。 code>else 如果该单元格不是最喜欢的,则将该图像设置为零。
You doing all of your cell configuration inside the
if (cell == nil)
statement. This code is only executed if the tableView did not return a cell from the dequeue method. What is probably happening is that you are re-using cells and they are not being configured, so the data and appearance remain the same.Your
cellForRowAtIndexPath
method should have the following structure:if (cell == nil)
statementOK, based on your updated code, the problem appears to be that you are setting the favourite image, but you don't unset it if the cell is no longer a favourite. You need an
else
to set that image to nil if the cell is not a favourite.