2013-04-24 119 views
0

我只是在我的應用程序中實現表格視圖。我已經多次這樣做了,但這次表格單元格顯示重複的數據和表格滾動的速度也不好。只需發佈我的代碼表。將意識到任何有助於防止細胞數據重複和滾動速度優化的優化。表格單元格的奇怪行爲

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    int count = [array count]; 

    if (count%3 == 0) 
    { 
     return count; 
    } 
    else 
    { 
     count = count/3+1; 
    } 
    NSLog(@"Row count is%i",count); 
    return count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *MyIdentifier = @"MyIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:MyIdentifier]; 
    } 
    UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 490, 221)]; 
    int x = 15; 
    int y = 15; 
    int p = 10; 
    int q = 10; 
    for (int i = 0; i < 3; i++) 
    { 
     if (indexPath.row*3+i < [array count]) 
     { 
      UIImageView *img = [[UIImageView alloc] init]; 
      UIButton *btn = [[UIButton alloc] init]; 
      [img setImageWithURL:[NSURL URLWithString:[array objectAtIndex:indexPath.row*3+i]] 
       placeholderImage:[UIImage imageNamed:@"India.png"] 
         success:^(UIImage *image) {}failure:^(NSError *error){}]; 
      img.frame = CGRectMake(x, y, 140, 201); 
      btn.frame = CGRectMake(p, q, 150, 211); 
      btn.tag = indexPath.row*3+i; 

      [btn setBackgroundImage:[UIImage imageNamed:@"Picframe_N.png"] forState:UIControlStateNormal]; 
      [btn setBackgroundImage:[UIImage imageNamed:@"Picframe_S.png"] forState:UIControlStateHighlighted]; 
      [btn addTarget:self action:@selector(Movie_detail:) forControlEvents:UIControlEventTouchUpInside]; 
      [testView addSubview:img]; 
      [testView addSubview:btn]; 

      x = x+160; 
      p = p+160; 
     } 
     else 
     { 
      NSLog(@"No data"); 
      break; 
     } 
    } 
    [cell addSubview:testView]; 
    return cell; 
} 

在細胞更新的事情是爲目的,以在單個細胞中添加3個按鍵。沒有其他的。

+0

您應該使用自定單元,而不是刪除所有子視圖。 – Bhavin 2013-04-24 07:15:25

+0

@Vin直到現在我還沒有接近自定義單元格。但是如果問題不解決,我也會這樣處理。 – 2013-04-24 07:54:34

回答

2

SUGGESTION

使用自定義單元格

修復

創建並添加子視圖是被列入了細胞被分配的括號內

if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:MyIdentifier]; 
     //CREATING AND ADDING SUBVIEW SHOULD BE HERE 
    } 
      //Change in values is taken care here 

原因

否則每一個子視圖被創建時,分配的所有對象,當表滾動上面添加

REFER

Apple Docs

+0

沒有幫助。當我滾動表格視圖時,它會在單元格中每次重複數據。 – 2013-04-24 07:33:43

+0

覆蓋自定義子類中的方法'prepareForReuse'? – 2013-04-24 08:18:06

+0

@singhSan:好吧,它重複了,因爲你在if循環中的值做了更改,而不是else。請參閱如果值需要更改,則僅將其置於else循環中 – 2013-04-24 09:06:40

0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *MyIdentifier = @"MyIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:MyIdentifier]; 

     UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 490, 221)]; 
    int x = 15; 
    int y = 15; 
    int p = 10; 
    int q = 10; 
    for (int i = 0; i < 3; i++) 
    { 
     if (indexPath.row*3+i < [array count]) 
     { 
      UIImageView *img = [[UIImageView alloc] init]; 
      UIButton *btn = [[UIButton alloc] init]; 
      [img setImageWithURL:[NSURL URLWithString:[array objectAtIndex:indexPath.row*3+i]] 
       placeholderImage:[UIImage imageNamed:@"India.png"] 
         success:^(UIImage *image) {}failure:^(NSError *error){}]; 
      img.frame = CGRectMake(x, y, 140, 201); 
      btn.frame = CGRectMake(p, q, 150, 211); 
      btn.tag = indexPath.row*3+i; 

      [btn setBackgroundImage:[UIImage imageNamed:@"Picframe_N.png"] forState:UIControlStateNormal]; 
      [btn setBackgroundImage:[UIImage imageNamed:@"Picframe_S.png"] forState:UIControlStateHighlighted]; 
      [btn addTarget:self action:@selector(Movie_detail:) forControlEvents:UIControlEventTouchUpInside]; 
      [testView addSubview:img]; 
      [testView addSubview:btn]; 

      x = x+160; 
      p = p+160; 
     } 
     else 
     { 
      NSLog(@"No data"); 
      break; 
     } 
    } 
    [cell addSubview:testView]; 
    } 

    return cell; 
} 

你不應該初始化您的cell所在塊以外的任何內容初始化,因爲當你scrolltableview方法cellForRowAtIndexPath被調用每次,和你的看法testview是越來越創建每次..

的單元初始化,你應該只更新的值在單元格中顯示外..

希望上述作品..

編輯 但更好的方法,我建議嘗試創建一個Custom Cell,讓您可以輕鬆地訪問添加到您的電池組件更新其值作爲你的tableview滾動s ..

謝謝。

+0

嗨,這將返回滾動單元格中的數據重複。 – 2013-04-24 07:27:30

0

你好這個問題是正常的,當你正在使用dequeueReusableCell

只是做出改變,因爲我在下面梅索德下面列出..

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *MyIdentifier = @"MyIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    UIView *testView; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:MyIdentifier]; 
     testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 490, 221)]; 
     testView.tag = 111; 
     [cell addSubview:testView]; 
     [testView release];//For Non ARC 
    } 
    else 
    { 
     testView = [cell.contentView viewWithTag:111]; 
     for(UIView * vv in [testView subViews]) 
     { 
      [vv removeFromSuperView]; 
     } 
    } 
    int x = 15; 
    int y = 15; 
    int p = 10; 
    int q = 10; 
    for (int i = 0; i < 3; i++) 
    { 
     if (indexPath.row*3+i < [array count]) 
     { 
      UIImageView *img = [[UIImageView alloc] init]; 
      UIButton *btn = [[UIButton alloc] init]; 
      [img setImageWithURL:[NSURL URLWithString:[array objectAtIndex:indexPath.row*3+i]] 
       placeholderImage:[UIImage imageNamed:@"India.png"] 
         success:^(UIImage *image) {}failure:^(NSError *error){}]; 
      img.frame = CGRectMake(x, y, 140, 201); 
      btn.frame = CGRectMake(p, q, 150, 211); 
      btn.tag = indexPath.row*3+i; 

      [btn setBackgroundImage:[UIImage imageNamed:@"Picframe_N.png"] forState:UIControlStateNormal]; 
      [btn setBackgroundImage:[UIImage imageNamed:@"Picframe_S.png"] forState:UIControlStateHighlighted]; 
      [btn addTarget:self action:@selector(Movie_detail:) forControlEvents:UIControlEventTouchUpInside]; 
      [testView addSubview:img]; 
      [testView addSubview:btn]; 

      x = x+160; 
      p = p+160; 
     } 
     else 
     { 
      NSLog(@"No data"); 
      break; 
     } 
    } 

    return cell; 
} 

試試這個....

+0

嗨這種方法在表格視圖中創建了很多空白單元格。所以沒有幫助。 – 2013-04-24 07:42:53

+0

你有正確的標籤嗎? – 2013-04-24 07:47:12

+0

嘗試爲不同的單元格視圖類型使用不同的cellIdentifiers,對於具有UIImageView的單元格,UIButton可以具有不同的CellIdentifier,對於其他單元格應該不同 – Firdous 2013-04-24 07:48:09

0

重複數據不同的單元格可能是由於相同的「CellIdentifier」導致的,請針對不同的單元格視圖類型嘗試使用不同的「CellIdentifier」。我可以看到不同的單元格視圖類型,因爲你的代碼中有一些條件塊。

static NSString *MyIdentifier; 

MyIdentifier = @"Type1"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:MyIdentifier]; 
    } 


MyIdentifier = @"Type2"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:MyIdentifier]; 
    } 
+0

不能用這種方法..這是不可能的添加每個細胞有很多標識符。 – 2013-04-24 07:52:10

+0

沒有那麼多,只是有不同的一個簡單的細胞和不同的一個有UIImageView等。只有兩種類型 – Firdous 2013-04-24 07:59:23

+0

我的所有單元格包含從起始圖像視圖。所以在這種情況下,解決方案是什麼? – 2013-04-24 08:07:12

1

您面臨的問題是您將UIView添加到每個進入屏幕的單元格。但是,您正在重複使用這些單元格,以便舊的「添加」視圖仍在其上。數據的重複是因爲滾動時單元格上有多個測試視圖層。

您應該創建一個包含您在每個cellforrow中添加的testview的CustomCell:或者在cellview仍然爲nil值時將testview添加到cellview,以便如果它被重用,您將不會添加對單元格的另一個測試視圖和它們重疊。這些例子是在這裏寫的其他答案,但在任何地方都可以在網上找到。

不推薦,但: 另一種快速而骯髒的解決辦法是剛剛從細胞(或您添加testview任何子視圖)for(UIView *v in [cell(.contentView) subviews]){ [v removeFromSuperView]; }

+0

我應該在哪裏應用? – 2013-04-24 09:02:59

+0

髒的修復程序應該在第一個addsubview之前應用:在您的cellforrow中: – 2013-04-24 09:09:12

+0

它在沒有數據的表格中添加空白單元格。所以沒有用。 – 2013-04-24 09:15:23