Ios UITableView可选列表
我正在尝试实现一个可选的列表视图。列表视图包含一些名称。您可以选择或取消选择多个名称。如果选择名称,单元格样式将更改为Ios UITableView可选列表,ios,objective-c,uitableview,selection,Ios,Objective C,Uitableview,Selection,我正在尝试实现一个可选的列表视图。列表视图包含一些名称。您可以选择或取消选择多个名称。如果选择名称,单元格样式将更改为UITableViewCellAccessoryCheckmark 但现在我有一个问题。在取消选择名称之前,必须在单元格上按多次。这是因为总是首先调用didselectrowatinexpath // cellForRowAtIndexPath function if ([assignedPlayers containsObject:player.persistentDa
UITableViewCellAccessoryCheckmark
但现在我有一个问题。在取消选择名称之前,必须在单元格上按多次。这是因为总是首先调用didselectrowatinexpath
// cellForRowAtIndexPath function
if ([assignedPlayers containsObject:player.persistentData])
{
[cell setSelected:true];
[cell setAccessoryType: UITableViewCellAccessoryCheckmark];
}
cell.textLabel.text = [NSString stringWithFormat:@"%@", player.persistentData.name];
return cell;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
[[tableView cellForRowAtIndexPath:indexPath] setSelected:FALSE];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType: UITableViewCellAccessoryNone];
[self.eventController removePlayerFromEvent:_editingEvent :[self.playerController getPlayerAtPosition:indexPath.row]];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[[tableView cellForRowAtIndexPath:indexPath] setSelected:TRUE];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType: UITableViewCellAccessoryCheckmark];
[self.eventController addPlayerToEvent:_editingEvent :[self.playerController getPlayerAtPosition:indexPath.row]];
}
现在我不知道如何解决这个问题
谢谢您的帮助。您需要使tableView支持多选。在
viewDidLoad
中尝试以下代码:
self.myTableView.allowsMultipleSelection = YES;
这样,在相应的代理中就不需要这两行:
[[tableView cellForRowAtIndexPath:indexPath] setSelected:TRUE];
[[tableView cellForRowAtIndexPath:indexPath] setSelected:FALSE];
对每个选定索引进行检查,例如:使用“作为选定索引” 否每次选择单元格时都将其添加到此数组,例如:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([selectedIndex containsObject:indexPath]) {
[selectedIndexes removeObject:indexPath];
}else{
[selectedIndexes addObject:indexPath];
}
[tableView reloadData];
}
然后在CellforRowatinex中
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([selectedIndex containsObject:indexPath]) {
[cell setSelected:TRUE];
[cell setAccessoryType: UITableViewCellAccessoryCheckmark];
[self.eventController addPlayerToEvent:_editingEvent :[self.playerController getPlayerAtPosition:indexPath.row]];
}
}else{
[cell setSelected:FALSE];
[cell setAccessoryType: UITableViewCellAccessoryNone];
[self.eventController removePlayerFromEvent:_editingEvent :[self.playerController getPlayerAtPosition:indexPath.row]];
}
}
这比从DidSelect方法获取单元格更有效,只需确保使用[tableView reloadData];在didSelectRowAtIndexPath中,如果不包含对象,则应将accessoryType设置为UITableViewCellAccessoryNone。 因为细胞可以重复使用。 在选择或取消选择时,不需要设置“已选择”
if ([assignedPlayers containsObject:player.persistentData]){
[cell setAccessoryType: UITableViewCellAccessoryCheckmark];
} else {
cell.accessoryType= UITableViewCellAccessoryNone;
}
请看下面的示例,其中
\u selectedList
包含选定的玩家,而\u datasourcearray
是包含所有玩家姓名的数据源
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
}
cell.textLabel.text = [_datasoureArray objectAtIndex:indexPath.row];
if([_selectedList containsObject:[_datasoureArray objectAtIndex:indexPath.row]]) //hear selected list is an mutable array wich holds only selected player
{
cell.selected = YES;
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.selected = NO;
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell)
{
if(cell.accessoryType == UITableViewCellAccessoryNone)
{
[_selectedList addObject:[_datasoureArray objectAtIndex:indexPath.row]];//add
}
else
{
if([_selectedList containsObject:[_datasoureArray objectAtIndex:indexPath.row]])
{
[_selectedList removeObject:[_datasoureArray objectAtIndex:indexPath.row]]; //remove
}
}
}
[tableView reloadData];
NSLog(@"%@",_selectedList.description);
}
您可以通过以下方式实现功能:, 当您选择一行时,您将检查数组中与该行对应的人员(如果该行已存在),然后将其从数组中删除,或者将其添加到数组中并重新加载表。在cellForRowAtIndexPath方法中,检查数组中是否有人存在,设置为selected true,否则设置为selected false //CellForRowatineXpath函数
{
if ([assignedPlayers containsObject:player.persistentData])
{
[cell setSelected:true];
[cell setAccessoryType: UITableViewCellAccessoryCheckmark];
}
else
{
[cell setSelected:Flase];
[cell setAccessoryType: UITableViewCellAccessoryCheckmarkNone];
}
cell.textLabel.text = [NSString stringWithFormat:@"%@", player.persistentData.name];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([assignedPlayers containsObject:player.persistentData])
{
[assignedPlayers removeObject:player.persistentData];
}
else
{
[assignedPlayers addObject:player.persistentData];
}
[tableView reloadData];
}
但是OP声明他们只想要单一选择。哦。。很抱歉当我看到复选标记时,我认为他需要多重选择。我通常会做多次选择。现在将删除答案。你的回答符合他的目的您为什么要在
row
方法中执行此操作?它应该在didSelectRow
方法中。OP似乎只需要一次选择,所以单元格没有自己的选择状态?我认为函数[cell setSelected:true]
应该这样做吗?@HotPizzaBox可能是..我不确定,但这个逻辑对我来说很有用..我可能在我的应用程序中多次使用它来完成类似于你所做的事情。我认为这种方法对于取消选择一个原始文件来说非常奇怪。您可以使用DIDDENCEROWATINDEXPATH
来执行取消选择单元格的任何操作。这个代码不应该在IMO中使用。@HarikrishnanT好的,你有更好的想法吗。我同意你的看法,但由于我的代码不起作用……你的开头一段说可以选择一个名字。这与最后的更新冲突。