iphone - UITableViewCell 附件类型在点击时选中并设置其他未选中

标签 iphone objective-c xcode uitableview

我对设置表格 View 单元配件有点困惑。

我在表格中修复了两个部分

  • 主页
  • 办公室

我想要的是如下......

  • 当用户点击任何单元格时
  • 单元格被选中 &
    • 我想设置选中(设置 uitableviewcell 附件类型 - 已选中点击单元格)
  • 所有其他单元格的附件类型现在应该设置为
    • 合适的 View 单元格附件类型无

我试过下面的代码。但是我发现 indexpath.row & indexpath.section 是只读属性。

// Override to support row selection in the table view.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {    
    [tblProfileView deselectRowAtIndexPath:indexPath animated:YES];
    int i,max;
    UITableViewCell *x; NSIndexPath *tt;

    for(i=0;i<[tblProfileView numberOfRowsInSection:0];i++)
    {
        tt.row=i; tt.section=0;
        x=[tblProfileView cellForRowAtIndexPath:];
        [x setAccessoryType:UITableViewCellAccessoryNone];
    }
    for(i=0;i<[tblProfileView numberOfRowsInSection:1];i++)
    {
        tt.row=i; tt.section=1;
        x=[tblProfileView cellForRowAtIndexPath:tt];
        [x setAccessoryType:UITableViewCellAccessoryNone];
    }

    x=[tblProfileView cellForRowAtIndexPath:indexPath];
    [x setAccessoryType:UITableViewCellAccessoryCheckmark];
    // Navigation logic may go here -- for example, create and push another view controller.
    // AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
    // [self.navigationController pushViewController:anotherViewController animated:YES];
    // [anotherViewController release];
}

最佳答案

我会跟踪应该检查的数据,并更改 tableView:didSelectRowAtIndexPath: 中的单元格,并更新在 tableView:cellForRowAtIndexPath: 中检查的数据,如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // do usual stuff here including getting the cell

    // determine the data from the IndexPath.row

    if (data == self.checkedData)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // determine the selected data from the IndexPath.row

    if (data != self.checkedData) {
       self.checkedData = data;
    }

    [tableView reloadData];
}

关于iphone - UITableViewCell 附件类型在点击时选中并设置其他未选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1750753/

相关文章:

swift - 连接不显示操作

ios - 如何在屏幕上已经有触摸的情况下激活 UIGestureRecognizer?

ios - 如何将文件下载链接到进度 View

ios - Swift - 从反向地理编码生成地址格式

iphone - 在 AlertDialog iPhone 中打开 WebView

objective-c - 谁是 Cocoa MVC 文档架构中的 Controller ?

ios - iOS上载带有视频信息GTLServiceYoutube的视频

iphone - UITableview 中的 UItextView,自动更正气泡不可见

iphone - 在 iPhone map 中的 route 添加注释

iphone - 如何从我的 iPhone 应用程序中找到整个设备的 CPU 消耗?