gpt4 book ai didi

IOS:在 uitableviewcell 中维护按钮状态

转载 作者:可可西里 更新时间:2023-11-01 06:19:22 24 4
gpt4 key购买 nike

我有一个 iPhone 应用程序问题已经困扰我好几天了,它看起来真的不应该这么困难,所以我确信我遗漏了一些明显的东西。我研究了很多关于“类似”主题的论坛讨论,但没有任何内容能真正具体地解决这个问题。

需要说明的是,如果我需要研究一些文档或其他来源,请指出正确的方向。

这里...

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

问题来了:

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

- (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];
}

所以,我已经经历了很多次迭代,但我似乎无法获得按钮的状态来显示那些被喜欢的项目的选定图像,并为那些没有被喜欢的项目保留正常图像喜欢。

如有任何帮助或建议,我们将不胜感激。

最佳答案

有一个简单的方法可以解决这个问题。您可以欺骗按钮以保持最新的选定状态。

制作一个可变数组,目的是保持按钮的选中状态

selectedButton = [[NSMutableArray alloc]init];//i've already defined the array at the .h file

for (int i = 0; i<yourTableSize; i++) //yourTableSize = how many rows u got
{
[selectedButton addObject:@"NO"];
}

在 tableviewcell 方法中,声明您的按钮并设置它,以便它引用可变数组以设置它的选定状态

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}

UIImage *img = [UIImage imageNamed:@"btn_unselected.png"];
UIButton *toggleButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)];
[toggleButton setImage:img forState:UIControlStateNormal];
img = [UIImage imageNamed:@"btn_selected.png"];
[toggleButton setImage:img forState:UIControlStateSelected];
[toggleButton setTag:indexPath.row+100];//set the tag whichever way you wanted it, i set it this way so that the button will have tags that is corresponding with the table's indexpath.row
[toggleButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:toggleButton];

//and now we set the button's selected state, everytime the table reuse/redraw the cell the button will set it's selected state according to the array

if([[selectedButton objectAtIndex:indexPath.row]isEqualToString:@"NO"])
{
[toggleButton setSelected:NO];
}
else
{
[toggleButton setSelected:YES];
}

return cell;

最后,创建当按钮被按下时按钮触发的方法,以改变它的选中状态

-(void)buttonPressed:(UIButton*)sender
{
int x = sender.tag - 100; //get the table's row
if([sender isSelected]) //if the button is selected, deselect it, and then replace the "YES" in the array with "NO"
{
[selectedButton replaceObjectAtIndex:x withObject:@"NO"];
[sender setSelected:NO];
}
else if (![sender isSelected]) //if the button is unselected, select it, and then replace the "NO" in the array with "YES"
{
[selectedButton replaceObjectAtIndex:x withObject:@"YES"];
[sender setSelected:YES];
}
}

关于IOS:在 uitableviewcell 中维护按钮状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9423152/

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