单选
来自:http://blog.sina.com.cn/s/blog_5d68044001017to3.html
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int newRow = [indexPath row];
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
if (newRow != oldRow)
{
UITableViewCell *newCell = [tableView cellForRowAtIndexPath indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPath = [indexPath copy];//一定要这么写,要不报错
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
注:lastindexpath 为 NSIndexPath * lastIndexPath; 定义的一个属性
多选
来自:忘了UItableView单选和多选 - Avivi*.* - Avivi*.*
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *oneCell = [tableView cellForRowAtIndexPath: indexPath];
NSLog(@"oneCell.tag :%d",oneCell.tag);
if (oneCell.accessoryType == UITableViewCellAccessoryNone)
{
oneCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
oneCell.accessoryType = UITableViewCellAccessoryNone;
}
}
来自:http://blog.sina.com.cn/s/blog_5d68044001017to3.html
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int newRow = [indexPath row];
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
if (newRow != oldRow)
{
UITableViewCell *newCell = [tableView cellForRowAtIndexPath indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPath = [indexPath copy];//一定要这么写,要不报错
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
注:lastindexpath 为 NSIndexPath * lastIndexPath; 定义的一个属性
多选
来自:忘了UItableView单选和多选 - Avivi*.* - Avivi*.*
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *oneCell = [tableView cellForRowAtIndexPath: indexPath];
NSLog(@"oneCell.tag :%d",oneCell.tag);
if (oneCell.accessoryType == UITableViewCellAccessoryNone)
{
oneCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
oneCell.accessoryType = UITableViewCellAccessoryNone;
}
}
本文介绍了如何在iOS开发中使用UITableView实现单选和多选功能。对于单选功能,通过更新选中项的索引路径并在选择新行时取消选中旧行来实现。而对于多选功能,则允许用户多次点击同一行来切换选中状态。
1108

被折叠的 条评论
为什么被折叠?



