2013-01-11 45 views
0

这里滚动2 3次完全向上和向下的tableview添加图像到所有单元格。下面是代码重复的UItableViewCell,UITableView的行

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

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

VenueDC *venueObj = [subSubCategoriesArray objectAtIndex:indexPath.row]; 
cell.lblName.text = venueObj.name; 
if ([venueObj.imagesArray count] > 0) { 
    [cell.ivVenue setImage:venueObj.ivVenue]; 
    [cell.ivVenue setHidden:NO]; 
    cell.lblName.frame = CGRectMake(80, cell.lblName.frame.origin.y, 200, cell.lblName.frame.size.height); 
} 
venueObj = nil; 


return cell; 
} 

什么这里发生的任何爱迪尔,图像仅在一个对象,第一次加载它显示了图像中的一个单元,但滚动后,对其他细胞也

+0

试试这个回答,可能会对你有帮助。 :http://stackoverflow.com/questions/10535637/reusablecellwithidentifier-issue-in-cellforrowatindexpath/10535788#10535788 – superGokuN

回答

3

解决像这样,第i个设置lblName帧到原来的位置,所以它重置其位置每次使用细胞

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

HomeCell *cell = (HomeCell *) [tableView dequeueReusableCellWithIdentifier:@"HomeCell"]; 

if (cell == nil) { 

    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"HomeCell" owner:self options:nil]; 

    for (id currentObject in topLevelObjects){ 
     if ([currentObject isKindOfClass:[UITableViewCell class]]){ 
      cell = (HomeCell *) currentObject; 
      break; 
     } 
    } 
} 

VenueDC *venueObj = nil; 
venueObj = [subSubCategoriesArray objectAtIndex:indexPath.row]; 
cell.lblName.text = venueObj.name; 
[cell.ivVenue setHidden:YES]; 
cell.lblName.frame = CGRectMake(20 , 19, 250, 20); 
if (venueObj.ivVenue) { 
    [cell.ivVenue setImage:venueObj.ivVenue]; 
    [cell.ivVenue setHidden:NO]; 
    cell.lblName.frame = CGRectMake(80, cell.lblName.frame.origin.y, 200, cell.lblName.frame.size.height); 
} 
return cell; 
} 
0

你开始显示图片需要一个else语句。尝试:

else { 
venueObj = nil; 
} 
2

UITableViewCells得到重用,这是dequeueReusableCellWithIdentifier什么:方法执行。为了节省内存,UITableView只能实例化任意多个单元格,因为它可以在任何给定的点上看到,然后继续重用它们。因此,您应该始终在重新使用之前清理您的单元格,即将其所有内容重置为nil

如果您正在使用自定义表格视图,如HomeCell那么覆盖 - (void)prepareForReuse并将图像设置为零。

希望有所帮助。

0

试试这个

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

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

VenueDC *venueObj = [subSubCategoriesArray objectAtIndex:indexPath.row]; 
cell.lblName.text = venueObj.name; 
    [cell.ivVenue setImage:nil]; 

if ([venueObj.imagesArray count] > 0) { 
    [cell.ivVenue setImage:venueObj.ivVenue]; 
    [cell.ivVenue setHidden:NO]; 
    cell.lblName.frame = CGRectMake(80, cell.lblName.frame.origin.y, 200, cell.lblName.frame.size.height); 
} 
venueObj = nil; 


return cell; 
} 
+0

现在它没有显示图像,但是,这张图片可以看出是隐藏的,所以它确实进入了“如果“并移动lblName转发 –

+0

检查图像是否存在或不..然后不设置lblname的框架。希望它有帮助 –

0

试试这个:

if(venueObj.ivVenue) 
{ 
     [cell.ivVenue setImage:venueObj.ivVenue]; 
} 
else 
{ 
     cell.ivVenue.image = Nil; 
} 
+0

你可以请你解释一下这是OP的问题的答案吗?这个答案因长度而被标记。 –

0

您只需添加一个行cellForRowAtIndexPath

NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row]; 

和更改

HomeCell *cell = (HomeCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

这可能对您有帮助;)

+0

这会产生内存问题。你怎么看?它不会重复使用任何单元格与另一个单元格,但在相同的部分具有相同的索引路径,从而创建与tableview中存在的行数不同的单元格。纠正我,如果我错了。 –

+0

是你写在这个代码它创建新的单元格时,用户滚动tableview不重用旧单元格;) – iPatel

+0

是否有必要?我们可以通过重复使用单元来创建相同的效果,从而减少内存使用量,然后此代码将占用.. –

1
it is happening because your cell is reuse so write this code 

HomeCell *cell = (HomeCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 

     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"HomeCell" owner:self options:nil]; 

     for (id currentObject in topLevelObjects){ 
      if ([currentObject isKindOfClass:[UITableViewCell class]]){ 
       cell = (HomeCell *) currentObject; 
       break; 
      } 
     } 
    } 
+0

你的答案帮助我upvote,但我得到解决的问题看到我的答案 –