IOS桌面选择行不工作
我想添加一个复选标记到选定的行,并将其删除时,再次选中,但每次我选择一行时,复选标记只会去第一个单元格,然后向下移动(即我点击任何单元格,然后单元格0有一个复选标记,我点击任何其他单元格,单元格1有复选标记,等等......)。IOS桌面选择行不工作
这里是我的didSelectRowAtIndexPath方法和方法的cellForRowAtIndexPath:
更新:
// the cell will be returned to the tableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"preflabel"forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"preflabel"];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
cell.textLabel.text = [NSString stringWithFormat:@"%@", data[indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"preflabel"forIndexPath:indexPath];
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
你有[的tableView reloadData]这将清除您的tableview的CURENT状态。
重新加载表视图会清除当前状态,包括当前选择。但是,如果您显式调用reloadData,则会清除此状态,并且对layoutSubviews的任何后续直接或间接调用都不会触发重新加载。
欲了解更多信息see.
更新
你正在创建在didSelectRowAtIndexPath方法和的cellForRowAtIndexPath tableviewcells。你为什么做这个?这将写入选定的单元格。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
删除它仍然不会为选定的行添加复选标记。该行只是突出了第二次然后unighighlights,这是它的结束,没有复选标记 – Minimi
看到我的更新... – sebenalern
完美,非常感谢你! – Minimi
你不应该获得通过双端队列方法细胞在选择的方法,因为你得到的细胞是不是你感动的电池,使用cellForCellAtIndexPath一样去选方法:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
}
因为reloadData的可能?重新加载表视图会清除当前状态,包括当前选择。但是,如果您显式调用reloadData,则会清除此状态,并且对layoutSubviews的任何后续直接或间接调用都不会触发重新加载。 – sebenalern
不要将您的代码作为图像发布。用实际的代码更新你的问题(并请正确格式化)。 – rmaddy
从'didSelectRowAtIndexPath'删除'[tableView reloadData]'。 – ivarun