iPhone sdk UITableView单元重用标识符

时间:2013-02-01 11:10:32

标签: iphone uitableview

我正在使用UITableView。当下载一本书时,我正在向我的表添加一个复选标记图像。我做到了这一点。但是当我滚动我的桌子时,即使对于未下载的书籍,我也会得到图像(即)我的表格单元格被重复使用。为此,我用Google搜索并检查了我的代码。一切似乎都是一样的。这是我的代码,

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

      UIImage *cellImage= [cacheImage getCachedImage:[listOfThumImages objectAtIndex:indexPath.row]];
      if(cellImage == nil)
{
    DeviantDownload *download;
    download = [DownloadsArray objectAtIndex:indexPath.row];
    cellImage = download.image;
    if (cellImage == nil)
    {
        download.delegate = self;
    }

NSLog(@"cellImage%@",cellImage);
UIImageView *imgView;


static NSString *CellIdentifier = @"";

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

} 

  imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(680, 60, 40, 40)];//self.view.bounds.size.width-
    [imgView1 setImage:[UIImage imageNamed:@"Downloaded.png"]];

  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if([defaults objectForKey:@"firstRun"])
    {
       if([[[appDelegate selectDB] valueForKey:@"bookname"] containsObject:[listOfBooks objectAtIndex:indexPath.row]] )
      {
           NSString *cellValue = [listOfBooks objectAtIndex:indexPath.row];
           cell.contentView addSubview:imgView1];
      }
   return cell;

    }
 }

}

我的代码出了什么问题?请帮助我。感谢你。

2 个答案:

答案 0 :(得分:0)

如果您希望单元格不显示图像,则只需删除该单元格的imageview。为图像视图设置view.tag,并检索它,然后将其删除。

您现在正在做的是,只要您需要为单元格添加图像子视图,然后将该单元格重新用于所有其他行。但是你从未删除过不需要它的细胞的imageview。

答案 1 :(得分:0)

使用UITableView的CheckMarks看起来并不复杂。首先,您必须保存已有书签的行的状态。并且,在您的cellForRowAtIndexPath方法中进行比较。您可以做的是在NSMutableArray上创建。为下载的书籍保存1,为未下载的书籍保存0。在cellForRowAtIndexPath方法比较[array objectAtIndex:indexpath.row]并相应地设置图像。

另外,改变:

static NSString *CellIdentifier = @"";

致:

static NSString *CellIdentifier = @"MyCell";
相关问题