2014-11-20 21 views
-1

更新UITableViewCell的自定義單元格我已經嘗試了多達解決方案,我發現來解決這一問題。 我最近的代碼是show blow:的UITableView不是從情節串連圖板

static NSString *simpleTableIdentifier = @"cell"; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

    [[cell viewWithTag:1000] removeFromSuperview]; 
     [[cell viewWithTag:2000] removeFromSuperview]; 

    // other code for setting view that works perfect then 
    if(some condition){ 
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom]; 
     [btn setImage:[UIImage imageNamed:@"upgrey.png"] forState:UIControlStateNormal]; 
     btn.frame=upButton.frame; 
     [btn addTarget:self action:@selector(upButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
     [cell addSubview:btn]; 
     UIButton *btn2=[UIButton buttonWithType:UIButtonTypeCustom]; 
     [btn2 setImage:[UIImage imageNamed:@"downgrey.png"] forState:UIControlStateNormal]; 
     btn2.frame=downButton.frame; 
     [btn2 addTarget:self action:@selector(downButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
     [cell addSubview:btn2]; 
     btn.tag=1000; 
     btn2.tag=2000; 
    } 

    return cell; 

但這不起作用。如果我添加 [[cell viewWithTag:1000] removeFromSuperview]; [[cell viewWithTag:2000] removeFromSuperview];

在開始從所有小區刪除按鈕。如果我不使用這個。它用這兩個按鈕顯示所有單元格。 在此先感謝。

+0

你想在哪裏顯示這些按鈕?你的結果正是我期望的結果。 – woz 2014-11-20 18:47:47

+0

如果條件是tru,我想將這些按鈕顯示爲tableview單元格的一部分。但如果我添加[[cell viewWithTag:1000] removeFromSuperview]; [[cell viewWithTag:2000] removeFromSuperview];線。它不顯示錶視圖中的按鈕,如果我不添加這些行。當我向上或向下滾動時,它顯示大多數表格單元格中的按鈕 – 2014-11-20 19:49:33

+0

右,使用'removeFromSuperview'刪除按鈕。 – woz 2014-11-20 19:58:33

回答

3

即使你已經接受了你自己的答案,我會花點時間來解釋爲什麼你有這樣的問題,以及如何更好地構建你UITableView S IN的未來。

當你沒有這些removeFromSubview行時,「顯示帶有這兩個按鈕的所有單元格」的原因是因爲通過實施dequeueReusableCellWithIdentifier:,您正在重新使用您的單元格及其內容。在每次調用cellForRowAtIndexPath期間反覆添加相同的子視圖會導致您重複使用單元格,因爲您完全沒有在其中重複使用單元格。因此,要麼,不要再用你的細胞,在所有的,或者更好的,因爲每個單元格的內容是完全一樣的,重用你的細胞和重複使用這些按鈕。

有兩個很好的方法可以做到這一點。一種方法是創建一個UITableViewCell自定義子類。但是由於你的單元格內容非常簡單,並且你說你的條件已經有效,所以我會提出一種不同的方式,可能會少一點代碼密集型。這是我的建議:

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

    // Create two identifiers -- one for "some condition" one 
    // for the "other condition" 
    static NSString *simpleTableIdentifier1 = @"cell1"; 
    static NSString *simpleTableIdentifier2 = @"cell2"; 

    if (some condition) { 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier1]; 

     // Only add the buttons when cell == nil, i.e. during the first 
     // time cell's initialized to hold a UITableViewCell 
     if (cell == nil) { 

      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier1] 

      UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom]; 
      [btn setImage:[UIImage imageNamed:@"upgrey.png"] forState:UIControlStateNormal]; 
      btn.frame=upButton.frame; 
      [btn addTarget:self action:@selector(upButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
      [cell addSubview:btn]; 
      UIButton *btn2=[UIButton buttonWithType:UIButtonTypeCustom]; 
      [btn2 setImage:[UIImage imageNamed:@"downgrey.png"] forState:UIControlStateNormal]; 
      btn2.frame=downButton.frame; 
      [btn2 addTarget:self action:@selector(downButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
      [cell addSubview:btn2]; 
     } 

    } else { 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier2]; 

     if (cell == nil) { 

      .... 
+0

這是一個很好的答案。因爲我用這種方式解決了同樣的問題。我正在作出正確的答案,因爲你非常精美地闡述了這個問題。 – 2014-11-29 08:21:56

+1

@ImranAhmed謝謝:) – 2014-11-29 14:59:00

相關問題