簡體   English   中英

UITableview不更新情節提要中的uitableviewcell自定義單元格

[英]UITableview not updating uitableviewcell custom cell from storyboard

我嘗試了盡可能多的解決方案來解決此問題。 我的最新代碼很受打擊:

  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];

首先,從所有單元格中刪除按鈕。 如果我不使用這個。 它使用這兩個按鈕顯示所有單元格。 提前致謝。

即使您已經接受了自己的答案,我也將花一點時間解釋為什么會遇到這個問題,以及將來如何更好地構造UITableView

當您沒有那些removeFromSubview行時,之所以會“顯示具有這兩個按鈕的所有單元格”,是因為通過實現dequeueReusableCellWithIdentifier:您正在重用單元格及其內容。 在每次調用cellForRowAtIndexPath過程中重復添加相同的子視圖,使得您完全沒有意義地重復使用單元格,因為您根本沒有在單元格中重復使用任何內容。 因此,要么,不要再用你的細胞,在所有的,或者更好的,因為每個單元格的內容是完全一樣的, 你的重用細胞, 重用這些按鈕。

有兩種好的方法可以做到這一點。 一種方法是創建UITableViewCell自定義子類。 但是由於單元格的內容非常簡單,並且您說else條件的功能已經起作用,所以我建議采用另一種方式,這種方式可能會減少代碼密集性。 這是我的建議:

- (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) {

            ....

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM