2012-12-23 28 views
2

大家好, 我正在嘗試添加與indexPath.row相關的標籤的自定義按鈕。如果我不在tableview中插入新行,我可以正確查看標記值。但是,當我插入新行時,如果插入的行不在0到9之間,那麼我的新行標記值是不正確的(iphone 5可以顯示出來)。在iphone上,我需要向下滾動才能看到。


但是,使用相同的代碼,我可以正確地在我的ipad上獲得我的標記值。我不需要向下滾動我的iPad上的表格視圖來查看我所有的行。我想知道爲什麼會發生,以及如何解決。在帶有標籤的tableview中添加自定義按鈕

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

    static NSString *CellIdentifier = @"dialoguesTableCell"; 
    dialoguesCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[dialoguesCell alloc] initWithStyle:UITableViewCellStyleDefault 
      reuseIdentifier:CellIdentifier]; 

    } 

    UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [yourButton setImage:[UIImage imageNamed:@"1StarBlank.png"]  forState:UIControlStateNormal]; 
    [yourButton setTitle:@"Button" forState:UIControlStateNormal]; 
    [yourButton addTarget:self action:@selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside]; 
    yourButton.tag=indexPath.row; 
    yourButton.frame = CGRectMake(5, 10, 40, 25); 
    [cell addSubview:yourButton]; 
    return cell; 


} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSIndexPath *indexPathstoinsert = [NSIndexPath indexPathForRow:indexPath.row+1  inSection:section]; 
    NSArray *indexPathsToInsertArray = [NSArray arrayWithObject:indexPathstoinsert]; 
    [[self mainTableView] insertRowsAtIndexPaths:indexPathsToInsertArray withRowAnimation:UITableViewRowAnimationRight]; 

} 
+0

請勿在您的帖子中使用簽名或標語,否則將被刪除。有關詳細信息,請參見[常見問題](http://stackoverflow.com/faq#signatures)。 – Artemix

回答

1

這是不正常工作,因爲UITableView的重用細胞
當你向下滾動,第一單元的arent看見了和被重用。

如果表..「小」你可以解決它通過不重複使用細胞
如果表心不是隻有幾個項目,但大量的數據,你真的想改變自己的方式

分配標籤在按鈕:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    UIButton *b = nil; 
    for(UIView *v in cell.subviews) { 
     if([v isKindOfClass:[UIButton class]]) { 
      b = (UIButton*)v; 
      break; 
     } 
    } 

    b.tag = indePath.row; 
} 

在commment

你提到另外一個問題:這些按鈕都隱藏在你的didSelectRow方法,並從其他細胞滾動後也隨後消失。同樣的問題:通過tableview,單元格對象是REUSED。不要在可重複使用的單元格中存儲狀態!

而是有記憶狀態的「模型」樹或陣列:文本,圖像,標籤,隱藏:是/否

NSArray *myTableContents

NSMutableDictionary *d1 = [@{@"text:@"bla", @"hidden":@NO} mutableCopy]; 
NSMutableDictionary *d2 = [@{@"text:@"bloo", @"hidden":@NO} mutableCopy]; 
NSMutableDictionary *d3 = [@{@"text:@"foo", @"hidden":@NO} mutableCopy]; 
myTableContents = @[d1,d2,d3]; 

THEN始終使用數組中numberOfRows和viewForRow並在didSelectEntry中修改它

+0

謝謝。它的工作。我wana做了與這種情況有關的另一個步驟。我試圖在新插入的行中隱藏該按鈕。但是,當我向上或向下滾動時,其他按鈕是隱藏的。我應該怎麼做? - (void)tableView :(UITableView *)tableView didSelectRowAtIndexPath :(NSIndexPath *)indexPath { NSIndexPath * favouriteIndexpath = [NSIndexPath indexPathForRow:indexPath.row inSection:section]; UITableViewCell * cellktl = [tableView cellForRowAtIndexPath:favouriteIndexpath]; UIButton * exerciseDate = [cellktl viewWithTag:indexPath.row]; exerciseDate.hidden = YES; } –

+0

同樣的原因,之後didSelect ..你需要一些「模型」要記住隱藏什麼行,然後重新在willDisplay ..生病多寫一些代碼,你 –

+0

由於細胞被重用。它會工作。你能不能多展示一下如何使用這個陣列?我應該寫在 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {..........} 然後,我不知道什麼是viewForRow。我想知道如何使用它。 –