2011-12-29 13 views
1

私のUITableViewでは、ユーザのイベントを取得する必要があるので、セルが選択(チェック)されたときにはbtn_checked.pngとし、チェックしないときはbtn_unchecked.pngとします。セルがチェックされたら画像を変更する

私は、私は何をしようとすると、didSelectRowAtIndexPath方法であるべき、と思うので、これは私のコードです:

- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSArray *listData =[self->tableContents objectForKey: 

    [self->sortedKeys objectAtIndex:[indexPath section]]]; 

    UITableViewCell* theCell = [tableView cellForRowAtIndexPath:indexPath]; 

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"btn_unchecked.png"]]; 

    theCell.accessoryView = imageView;  
} 

私のスニペットコードは動作しませんので、とき行btw_unchecked.pngが配置されていませんチェックされている、私は正しいイベントに右のイメージを配置するチェック/未チェックのイベントを取得する必要があります。 Thanxは事前に

答えて

-1

次のコードを使用できます。セルをタップしたとき、私はこれが例として..

1

をあなたを助けることを願っています

[cell.contentView addSubview:imageView]; 

、このコードスニペットは、セルのチェックマークを切り替えます。このパターンは、デリゲートメソッドが呼び出されると、表のデータソースから選択的にリロードするように強制します。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Dequeue the cell... then: 
    static NSString *CellIdentifier = @"tvcOEList"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    cell.accessoryType = (cell.accessoryType == UITableViewCellAccessoryNone) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES]; 
} 
関連する問題