IOS:维护uitableviewcell中的按钮状态 [英] IOS: Maintaining button state in uitableviewcell

查看:107
本文介绍了IOS:维护uitableviewcell中的按钮状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iPhone应用程序问题一直困扰着我几天,看起来真的不应该这么难,所以我肯定我错过了一些明显的东西。我已经研究了很多关于类似主题的论坛讨论,但实际上并没有解决这个问题,特别是。

I have an iPhone app problem that's been bugging me for a few days and it really doesn't seem like it should be this difficult so I'm sure I'm missing something obvious. I have researched plenty of forum discussions on "similar" topics but nothing that actually addresses this issue, specifically.

要清楚,如果有一些文档或某些文档我应该研究的其他来源,请指出正确的方向。

To be clear, if there is some piece of documentation or some other source that I should research, please point me in the right direction.

这里有...

我有一个项目列表,我在表格中显示给用户(uitableview)。每个项目的单元格(uitableviewcell)是自定义的,包含图像和Like按钮(uibutton)。正如所料,对于表中的每个项目,用户可以单击赞按钮或忽略它。 like按钮调用单独的进程来更新服务器。简单,对吧?

I have a list of items that I display to the user within a table (uitableview). The cell (uitableviewcell) for each item is custom and contains an image and a Like button (uibutton). As expected, for each item in the the table, the user can click the Like button or ignore it. The like button calls a separate process to update the server. Simple, right?

所以,问题在于:

当点击特定按钮时单元格,选定状态工作正常,但是当您将单元格滚出视图时,表格中的其他随机单元格将相似按钮显示为已选择,即使它们从未被触摸过。因为细胞被重复使用,出于明显的性能原因,我理解为什么会发生这种情况。我不明白的是为什么我的方法(见下面的代码)不会像我认为的那样覆盖或重置按钮的状态。为简洁起见,我只在此处包含相关代码(希望格式正确):

When the Like button is clicked on a particular cell, the Selected state works fine but the moment you scroll the cell out of view, other random cells in the table show the Like button as Selected even though they were never touched. Because the cells are reused, for obvious performance reasons, I understand why this could happen. What I don't understand is why my approach (see code below) would not override or reset the button's state the way I think it should. For brevity, I am only including the relevant code here (hopefully formatted properly):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MyCustomCell";
    MyCustomViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[MyCustomViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
}

    NSString *myRating = [[self.dataArray objectAtIndex:indexPath.row] valueForKey:@"my_rating"];

    // Create the Like button
    UIButton *likeButton = [[UIButton alloc] initWithFrame:CGRectMake(260, 68, 40, 40)];
    [likeButton setImage:[UIImage imageNamed:@"thumbsUp"] forState:UIControlStateNormal];
    [likeButton setImage:[UIImage imageNamed:@"thumbsUpSelected"] forState:UIControlStateSelected];
    if (myRating == @"9") {
        [likeButton setSelected:YES];
    }
    [likeButton setTitle:@"9" forState:UIControlStateNormal];
    [likeButton setTag:indexPath.row];
    [cell.contentView addSubview:likeButton];
    [likeButton addTarget:self action:@selector(likeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

- (void)likeButtonPressed:(UIButton *)sender {

    // Changed the Selected state on the button
    UIButton *button = (UIButton *)sender;
    button.selected = !button.selected;

    // Create a new object with the user's rating and then replace it in the dataArray
    NSString *ratingText = sender.titleLabel.text;    
    NSMutableArray *myMutableArray = [[self.dataArray objectAtIndex:row] mutableCopy];
    [myMutableArray setValue:ratingText forKey:@"my_rating"];
    [self.dataArray replaceObjectAtIndex:row withObject:myMutableArray];
}

所以,我经历了很多迭代,但我不能似乎得到了按钮的状态,以显示那些喜欢的项目的选定图像,并保留那些尚未被喜欢的项目的正常图像。

So, I've been through many iterations of this but I can't seem to get the state of the button to show the Selected image for those items that are Liked and keep the normal image for those items that have not been Liked.

任何帮助或建议将不胜感激。

Any help or advice would be greatly appreciated.

推荐答案

问题在于每次创建或重复使用一个单元格时,你都会给它一个新的喜欢按钮,所以当你重复使用类似按钮被激活的单元格时,你会给它一个停用的按钮,但旧的,激活的按钮仍然存在。

The problem is that every time you create or reuse a cell you're giving it a new like button, so when you reuse a cell where the like button has been activated, you're giving it a deactivated like button but the old, activated like button is still there as well.

您不必每次需要单元格时都创建类似按钮,而只需设置现有类似按钮的状态。请参阅这个问题的答案对于一些可能的处理方式。

Instead of creating a like button every time you need a cell, you should just be setting the state of an existing like button. See the answers to this question for some possible ways of handling that.

这篇关于IOS:维护uitableviewcell中的按钮状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆