2011-12-29 63 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

您可以使用以下代碼。

[cell.contentView addSubview:imageView]; 

我希望這將幫助你..

1

作爲一個例子,這個代碼片段將切換單元的對號,當細胞被竊聽。該模式是在調用代理方法時強制從表數據源中進行選擇性重新加載。

- (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]; 
} 
相關問題