OStack程序员社区-中国程序员成长平台

标题: iphone - 滚动离开屏幕时 UITableViewCell 附件消失 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 17:16
标题: iphone - 滚动离开屏幕时 UITableViewCell 附件消失

我有一个装满对象的 UITableView。在 didSelectRowAtIndexPath 方法中,我有一个 UITableViewCellAccessoryCheckmark 在选中行时出现,在未选中时消失。

这是 didSelectRowAtIndexPath 方法的代码:

- (void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
 UITableViewCell *curCell = [beerTable cellForRowAtIndexPath:indexPath];

if (curCell.accessoryType == UITableViewCellAccessoryCheckmark) {
    [curCell setAccessoryType:UITableViewCellAccessoryNone];
    compareCount = (compareCount - 1);

    if (tableView == [[self searchDisplayController] searchResultsTableView]) {
        NSString *objBeer = [searchResults objectAtIndex:indexPath.row];
        [compareBeers removeObject:[searchResults objectAtIndex:indexPath.row]];
        [compareCarbs removeObject:[carbAmount objectAtIndex:[beerNames indexOfObjectbjBeer]]];
    }
    else {
        [compareBeers removeObject:[beerNames objectAtIndex:indexPath.row]];
        [compareCarbs removeObject:[carbAmount objectAtIndex:indexPath.row]];
    }

}
else {
    [curCell setAccessoryType:UITableViewCellAccessoryCheckmark];
    compareCount = (compareCount + 1);

    if (tableView == [[self searchDisplayController] searchResultsTableView]) {
        NSString *objBeer = [searchResults objectAtIndex:indexPath.row];
        [compareBeers addObject:[searchResults objectAtIndex:indexPath.row]];
        [compareCarbs addObject:[carbAmount objectAtIndex:[beerNames indexOfObjectbjBeer]]];
    }
    else {
        [compareBeers addObject:[beerNames objectAtIndex:indexPath.row]];
        [compareCarbs addObject:[carbAmount objectAtIndex:indexPath.row]];
    }

}

if (compareCount > 0) {
    if (compareOn == YES){
    }
    else {
    compareButton.enabled = YES;
    UIImage *image = [UIImage imageNamed"redbutton.png"];
    [compareButton setImage:image];
    }
}

else {
    compareButton.enabled = NO;
    [compareButton setImage:nil];
    [compareButton setCustomView:nil];
}

}

我也有这个作为我的cellForIndexPath:

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath {


static NSString *CellIdentifier = @"CustomCell";

CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {

    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed"CustomCell" owner:self options:nil];

    for (id currentObject in topLevelObjects){
        if ([currentObject isKindOfClass:[UITableViewCell class]]){
            cell =  (CustomCell *) currentObject;
            break;
        }
    }
}

// Setting separate tables correctly....


return cell;
}

My problem is that when the cell that is selected is scrolled out of view the checkmark associated with that value is now gone when back into view.

我应该怎么做才能使复选标记不消失?

谢谢



Best Answer-推荐答案


当您滚动浏览数据时,您的单元格会被重新使用(这就是 dequeueReusableCellWithIdentifier 所做的)。在 didSelectRowAtIndexPath 中获得复选标记的单元格将被回收用于不同的行,并且不再与选中的行有任何连接。

您需要在CellForrowatIndExpath中设置/解开附件 View ,因此当签到的行滚动回到 View 中时,它们会得到适当检查。

关于iphone - 滚动离开屏幕时 UITableViewCell 附件消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5827034/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4