2009-04-13 125 views
0

對於表格視圖單元格中的每個條目,我需要一個按鈕在右邊,然後是一些文本,再按一下左邊的按鈕。在按鈕點擊事件時,我需要更改文本(點擊左/右按鈕),並根據文本條件刪除任一按鈕。我無法使用cellForRowAtIndexPath方法刪除按鈕。我試着繼承UITableViewCell並使用方法-prepareForReuse,但我無法重置單元格。任何想法我怎麼做到這一點?或者有什麼辦法讓這個按鈕不可見或可能隱藏?UITableViewCell沒有得到更新

NSString *CellIdentifier = [[NSString alloc] initWithFormat:@"Cell%d",indexPath.row]; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
} 


[self.tableView clearsContextBeforeDrawing]; 
NSString *str = [listOfItems objectAtIndex:indexPath.row]; 

    cell.text = [listOfItems objectAtIndex:indexPath.row]; 
    cell.textColor = [UIColor blueColor]; 
    cell.font = [UIFont systemFontOfSize:14]; 

     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
     UIImage *image = [UIImage imageNamed:@"Arrow_Right.png"]; 
     CGRect frame = CGRectMake(290, 5, image.size.width, image.size.height); 
     button.frame = frame; 
     [button setBackgroundImage:image forState:UIControlStateNormal]; 
     button.backgroundColor = [UIColor clearColor]; 
     [button addTarget:self action:@selector(rightArrow_clicked:) forControlEvents:UIControlEventTouchUpInside]; 
     [cell addSubview:button]; 

     UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
     UIImage *img = [UIImage imageNamed:@"Arrow_Left.png"]; 
     CGRect frame1 = CGRectMake(5, 5, img.size.width, img.size.height); 
     button1.frame = frame1; 
     [button1 setBackgroundImage:img forState:UIControlStateNormal]; 
     button1.backgroundColor = [UIColor clearColor]; 
     [button1 addTarget:self action:@selector(leftArrow_clicked:) forControlEvents:UIControlEventTouchUpInside]; 
     [cell addSubview:button1]; 

    if([str isEqual:@" abc "]) 
     [button setEnabled:NO]; 
    if([str isEqual:@" pqr "]) 
     [button1 setEnabled:NO]; 
+0

是的,它是可重複使用的單元格。如何在-prepareForReuse方法中重置它? – Neo 2009-04-13 13:06:51

回答

1

什麼你想要做的是子類的UITableViewCell,並在init方法,以便他們只加一次添加子視圖。

最可能發生的是您將子視圖添加到可重複使用的單元格中,該單元格很可能已經具有上一次使用的按鈕。此外,您可以將按鈕設置爲「不可見」,如下所示:

[button setHidden:YES];