alllllllll 2013-05-09 05:27 采纳率: 0%
浏览 1960
已采纳

在滚动表视图时选择目录重复

在一个表视图中显示信息,信息是固定的。使用逻辑将选择目录选择特定的空间。代码如下:

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
      *)indexPath {
    static NSString *CellIdentifier = @"CellIdentifier";
    // intializing tableview cell.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
        reuseIdentifier:CellIdentifier] autorelease];
    }
    // Setting tableviewcell title from Room array.

    cell.textLabel.text = [NSString stringWithFormat:@"%@",[[arr_roomList
     objectAtIndex:indexPath.row] valueForKey:@"description"]];

    /* checking the condition if checkedIndexPath is != null.
     means at first time checkedIndexPath= null.*/
    if (self.checkedIndexPath) {

        /* checking the condition if current cell is selected then
         we have to show the UITableViewCellAccessoryCheckmark (checkmark on right side of the
        cell).*/
        if([self.checkedIndexPath isEqual:indexPath])
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    else{
        if ([[[arr_roomList objectAtIndex:indexPath.row] valueForKey:@"resource_id"]
       isEqualToString:self.str_selected_resourceId]) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    }
    // This Method will set the Background and Selected Background Image for the cell.
    [[AppDelegate sharedInstance] SetBackgroundImageForcell:cell];
    if ([[[arr_roomList objectAtIndex:indexPath.row] valueForKey:@"rec_type"] isEqualToString:@"R"]) {
        cell.backgroundColor = [UIColor grayColor];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    /* checking the condition if checkedIndexPath is != null.
     means at first time checkedIndexPath= null.*/
    if(self.checkedIndexPath)
    {
        UITableViewCell* uncheckCell = [tableView
     cellForRowAtIndexPath:self.checkedIndexPath];
        uncheckCell.accessoryType = UITableViewCellAccessoryNone;
    }
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    self.checkedIndexPath = indexPath;
    [self changeRoomWithResourceId:[[arr_roomList objectAtIndex:indexPath.row] 
    valueForKey:@"resource_id"]];
  }

在往下滚动表视图时,选择目录会随机在单元中自身重复。请帮忙,谢谢。

  • 写回答

1条回答 默认 最新

  • AldisZhan 2013-05-09 16:11
    关注
       if ([[[arr_roomList objectAtIndex:indexPath.row] valueForKey:@"resource_id"]
       isEqualToString:self.str_selected_resourceId]) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    

    checkedIndexPath为nil时 多个room的resource_id 都等于self.str_selected_resourceId 会导致
    多个cell被标记check mark

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿