我对设置表格 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/