代码之家  ›  专栏  ›  技术社区  ›  zpasternack

UITableView,在选择时更换附件时出现问题

  •  0
  • zpasternack  · 技术社区  · 17 年前

    UIViewController子类是我的表的委托和数据源(不是UITableViewController,因为我在其中还有一个工具栏)。

    我想我可以关闭动画,但这似乎是站不住脚的。我还在didSelectRowAtIndexPath:中尝试获取和改变细胞,但这是一个巨大的痛苦。

    有什么想法吗?缩写代码如下。。。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell* aCell = [tableView dequeueReusableCellWithIdentifier:kImageCell];
        if( aCell == nil ) {
            aCell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:kImageCell];
        }
    
        aCell.text = [imageNames objectAtIndex:[indexPath row]];
        if( [indexPath row] == selectedImage ) {
            aCell.accessoryType = UITableViewCellAccessoryCheckmark;
        } else {
            aCell.accessoryType = UITableViewCellAccessoryNone;
        }
        return aCell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        selectedImage = [indexPath row]
        [tableView reloadData];
    }
    
    7 回复  |  直到 17 年前
        1
  •  8
  •   Ron Srebro    17 年前

    我像哈芬奇在编辑中建议的那样解决了这个问题。我的代码示例确保只检查了一行,但我确信如果这不是您需要的,您可以调整它。

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    int newRow = [indexPath row];
    int oldRow = [lastIndexPath row];
    
    if (newRow != oldRow)
    {
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
                                    indexPath];
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
    
        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:
                                    lastIndexPath];
        oldCell.accessoryType = UITableViewCellAccessoryNone;
    
        lastIndexPath = indexPath;
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    
        2
  •  4
  •   hatfinch    17 年前

    我怀疑唯一的方法是,正如你所建议的,保留对单元格本身的引用缓存。如果您编写一个UITableViewCell子类来封装逻辑,重写-prepareforuse,这应该不会太混乱。

    不过,这并不漂亮,如果苹果公司的铃声人员不得不这样做,他们应该更努力地敦促UITableView团队解决这个问题!

    你知道什么?!这毕竟很容易-[UITableView CellForRowatineXpath]将为您提供实际的单元格(如果在屏幕外,则为零),您只需更改附件类型即可。

        3
  •  2
  •   Michael D. Irizarry    16 年前

    我有一个更干净的方法。

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
        if (cell.accessoryType == UITableViewCellAccessoryNone) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
    
        } else {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    
        4
  •  2
  •   Evgen Bodunov    15 年前

    我使用这个简单而好的工作机制来处理重载rowsatindexpath。

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        NSArray *rowsArray = [NSArray arrayWithObjects:indexPath, selectedIndexPath, nil];
        self.selectedIndexPath = indexPath;
    
        [tableView reloadRowsAtIndexPaths:rowsArray withRowAnimation:UITableViewRowAnimationNone];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        .... skipped usual code .....
    
        if ([selectedIndexPath compare:indexPath] == NSOrderedSame) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        } else {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        return cell;
    }
    
        5
  •  1
  •   Bill DeWeese    16 年前

    这将按需要工作。。。

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    int newRow = [indexPath row];
    int oldRow = [lastIndexPath row];
    
    
    if (newRow == oldRow){
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
        if (cell.accessoryType == UITableViewCellAccessoryNone) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
    
        } else {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    
         lastIndexPath = indexPath;
    }
    
    
    if (newRow != oldRow)
    {
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
    
        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath];
        oldCell.accessoryType = UITableViewCellAccessoryNone;
    
        lastIndexPath = indexPath;
    }
    
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    }
    
        6
  •  0
  •   dandan78 Tom Cool    15 年前
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:index];
        oldCell.accessoryType = UITableViewCellAccessoryNone;
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        index = indexPath;  
    }
    
    推荐文章