0

「UITableViewCell」で「iphone-checkMark@2x.png」というチェックマークのカスタム画像を使用していますが、セル内のどこにでも表示されています。選択した単一のセルのみに表示する方法を誰かが提案できますか?

これが私のコードです:

 - (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;
}
4

2 に答える 2

1

コードが少ないほど良い:

- (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"];
于 2013-06-03T20:16:57.807 に答える