2013-04-24 121 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