我有一个 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) 是自定义的,包含一个图像和一个赞按钮 (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?
那么,问题来了:
当在特定单元格上单击 Like 按钮时,Selected 状态可以正常工作,但是当您将单元格滚动到视图之外时,表格中的其他随机单元格将 Like 按钮显示为 Selected,即使它们从未被触摸过.由于单元被重复使用,出于明显的性能原因,我理解为什么会发生这种情况.我不明白为什么我的方法(见下面的代码)不会像我认为的那样覆盖或重置按钮的状态.为简洁起见,我只在此处包含相关代码(希望格式正确):
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.
问题是每次你创建或重用一个单元格时,你都会给它一个新的like按钮,所以当你重用一个有like按钮的单元格时已激活,您正在给它一个停用的类似按钮,但旧的、已激活的类似按钮仍然存在.
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中保持按钮状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!