gpt4 book ai didi

ios - 重用 UITableViewCells 来显示喜欢的状态?

转载 作者:行者123 更新时间:2023-11-29 01:59:45 25 4
gpt4 key购买 nike

我有一个包含大量自定义单元格的 UITableView。每个单元格都有一个实用程序按钮,因此当您从右向左滑动单元格时,您可以选择收藏删除

此外,当选择收藏夹时,utilityButton 中的收藏夹按钮将变为红色,否则按钮为白色

问题是在我滚动 tableView 之后,我选择的单元格最喜欢的按钮(它们曾经是RED)变成了WHITE。我想我在我的细胞重用方法中遗漏了一些重要的东西。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ColorCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[cell setNeedsUpdateConstraints];
[cell setRightUtilityButtons:[self rightButtons] WithButtonWidth:58.0f];
cell.delegate = self;
return cell;
}

- (NSArray *)rightButtons
{
NSMutableArray *rightUtilityButtons = [NSMutableArray new];
[rightUtilityButtons sw_addUtilityButtonWithColor:[UIColor colorWithRed:0.78f green:0.78f blue:0.8f alpha:1.0] icon:[UIImage imageNamed:@"heart"]];
[rightUtilityButtons sw_addUtilityButtonWithColor:[UIColor colorWithRed:1.0f green:0.231f blue:0.188 alpha:1.0f] icon:[UIImage imageNamed:@"trash"]];
return rightUtilityButtons;
}

这是单元格的委托(delegate)方法,当触发 utilityButton 时将被调用并显示我如何设置所选按钮的图像,而 heartWHITEheart-选择RED。请注意,index == 0 是最喜欢的按钮,后面是删除按钮。

enter image description here

- (void)swipeableTableViewCell:(ColorCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index
{
NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell];
ColorModel *model = [self.objects objectAtIndex:cellIndexPath.row / 2];
if (cellIndexPath.row % 2 == 0) {
switch (index) {
case 0:
{
if ([self.favouriteArray containsObject:model]) {
[self.favouriteArray removeObject:model];
[cell.rightUtilityButtons.firstObject setImage:[UIImage imageNamed:@"heart"] forState:UIControlStateNormal];
}else{
[self.favouriteArray addObject:model];
[cell.rightUtilityButtons.firstObject setImage:[UIImage imageNamed:@"heart-selected"] forState:UIControlStateNormal];
}
[cell hideUtilityButtonsAnimated:YES];
break;
}
case 1:
{
[self.objects removeObjectAtIndex:cellIndexPath.row / 2];
[self.tableView reloadData];
break;
}
}
}
}

谢谢。

最佳答案

当您滚动表格 View 时,它会重新加载其单元格,并且在 cellForRowAtIndexPath 方法中,您正在重置/分配一组新的实用程序按钮,在这种情况下,心脏按钮是白色的

[cell setRightUtilityButtons:[self rightButtons] WithButtonWidth:58.0f];

您可以像下面这样编写 rightButtons 方法来代替它,它将解决问题。您也不需要监听 didTriggerRightUtilityButtonWithIndex

- (NSArray *)rightButtonsForIndexPath:(NSIndexPath *)indexpath
{
NSMutableArray *rightUtilityButtons = [NSMutableArray new];
if (/* row is selected*/) {
[rightUtilityButtons sw_addUtilityButtonWithColor:[UIColor colorWithRed:0.78f green:0.78f blue:0.8f alpha:1.0] icon:[UIImage imageNamed:@"heart-selected"]];
} else {
[rightUtilityButtons sw_addUtilityButtonWithColor:[UIColor colorWithRed:0.78f green:0.78f blue:0.8f alpha:1.0] icon:[UIImage imageNamed:@"heart"]];
}

[rightUtilityButtons sw_addUtilityButtonWithColor:[UIColor colorWithRed:1.0f green:0.231f blue:0.188 alpha:1.0f] icon:[UIImage imageNamed:@"trash"]];
return rightUtilityButtons;
}

关于ios - 重用 UITableViewCells 来显示喜欢的状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30495729/

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