所选行的自定义复选标记 [英] Custom Check mark for selected Row

查看:29
本文介绍了所选行的自定义复选标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UITableViewCell"中使用自定义图像作为复选标记 - 'iphone-checkMark@2x.png',但它显示在单元格中的任何地方.任何人都可以建议如何使其仅显示单个选定的单元格?

I am using a Custom image for checkmark - 'iphone-checkMark@2x.png' in a `UITableViewCell, but it's displaying everywhere in the cell. Can anyone suggest how to make it display only for the single selected cell?

这是我的代码:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                         reuseIdentifier:CellIdentifier];

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,10, 300, 30)];
    [cell.contentView addSubview:label];
    label.text = [tableArr objectAtIndex:indexPath.row];
    label.font = [UIFont fontWithName:@"Whitney-Light" size:20.0];
    label.tag = 1;
    UIImageView *checkmark = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"iphone-checkMark@2x.png"]];
    cell.accessoryView = checkmark;
    return cell;
}

推荐答案

代码越少越好:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                         reuseIdentifier:CellIdentifier];
 [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}


cell.textLabel.text = [tableArr objectAtIndex:indexPath.row];
cell.textLabel = [UIFont fontWithName:@"Whitney-Light" size:20.0];
return cell;

}

更新:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  lastIndexPath = indexPath;
  [tableView reloadData];
}


- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
            if ([lastIndexPath isEqual:indexPath]) {
                cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"iphone-checkMark.png"]];
            } else {
                cell.accessoryView = nil;
            }    
}

更新 2:

//dont forget check for nil
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:lastIndexPath forKey:@"lastIndexPath"]; 
[defaults synchronize];

阅读

NSIndexPath *lastPath = [defaults valueForKey:@"lastIndexPath"];

这篇关于所选行的自定义复选标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆