<span style="font-size:24px;">- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
NSString *s1 = cell.textLabel.text;
[user setObject:s1 forKey:@"selected"];
if (indexPath.section == 2|indexPath.section == 3) {
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[cell setSelected:YES animated:YES];
}
}
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
NSString *s2 = [user objectForKey:@"selected"];
if (indexPath.section == 2|indexPath.section == 3) {
if (s2 != cell.textLabel.text) {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
}
</span>
刚解决了一个困扰了一天的难题 ,让tableview的row只能有一个被选中,选中一个后其他的取消选中
<span style="font-size:24px;">- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
NSString *s1 = cell.textLabel.text;
[user setObject:s1 forKey:@"selected"];
if (indexPath.section == 2|indexPath.section == 3) {
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[cell setSelected:YES animated:YES];
}
}
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
NSString *s2 = [user objectForKey:@"selected"];
if (indexPath.section == 2|indexPath.section == 3) {
if (s2 != cell.textLabel.text) {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
}
</span>
使用了
NSUserDefaults
应该还有更简单的方法往告知!
再稍微简化点是
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.textColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
if (cell.selected == YES) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.textColor = [UIColor blackColor];
if (cell.selected == NO) {
cell.accessoryType = UITableViewCellAccessoryNone;
}else{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}