1

我是 iphone 新手。我在我的项目中遇到了一些任务(即),我有一个 66 行的表格视图。我为每个单元格放置了不同的书名,并为每本书放置了一个下载按钮。我的要求是当我们单击下载按钮时,它仅在该特定单元格中显示进度视图,但我正在进入该特定单元格,但是当我拖动表格视图时,它也会在某些单元格中显示进度视图。这是因为出列可重用性概念,但我不知道如何避免这个问题。我希望即使在拖动 tableview 之后它也会在单元格上显示进度视图,我单击下载按钮(单元格)

这是我下面的代码..

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{


    return 66;

}

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UIButton *downloadButton = nil;
    CustomCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) 
    {
  //here custom cell is another class in that we have the title label declaration
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
         downloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
        downloadButton.frame = CGRectMake(220,10,50,30);
        [downloadButton setImage:[UIImage imageNamed:@"download.png"] forState:UIControlStateNormal];
        [downloadButton addTarget:self action:@selector(downloadButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        downloadButton.backgroundColor = [UIColor clearColor];
        downloadButton.userInteractionEnabled = YES;
        downloadButton.highlighted = YES;
        downloadButton.tag = indexPath.row;
        NSLog(@"tag is %d",indexPath.row);
        [cell.contentView addSubview:downloadButton];
}    


   NSString *titleLabel = [[appDelegate getBookNames]objectAtIndex:indexPath.row];
    cell.TitleLabel.text = titleLabel;


    return cell;
}

-(void)downloadButtonClicked:(id)sender{
  int index = [sender tag];
    NSLog(@"index of the cell is %d",index);
    UIButton *button = (UIButton*)sender;

    UITableViewCell *cell = (UITableViewCell *)[[button superview] superview];

    UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];

    NSLog(@"label text =%@",titleLabel.text);
    selectedBookTitle = titleLabel.text;
    NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSMutableArray *allDownloadLinks;
    biblePlayerViewController = [[BiblePlayerViewController alloc]init]; 
   allDownloadLinks = [biblePlayerViewController allDownloadLinks];
    NSLog(@"all Download Links are %@",allDownloadLinks);
    biblePlayerViewController.indexOfSelectedBookTitle = [[appDelegate getBookNames]indexOfObject:selectedBookTitle];


    Download* download = [Download downloadWithTitle:selectedBookTitle url:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.audiotreasure.com/%@.zip",[allDownloadLinks objectAtIndex:(biblePlayerViewController.indexOfSelectedBookTitle)]]]PathtoSave:documentsPath];
    [[DownloadManager sharedDownloadManager] queueDownload: download];

   UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];

    progressView.frame = CGRectMake(10, 40, 300, 20);
    [tableViewCell.contentView addSubview:progressView];

}

我的项目的屏幕截图是[我在模拟器中的上述代码的输出]

4

2 回答 2

0

您应该每次在 cellForRow 中将单元格设为 nil。这样它就不会每次都被重复使用和分配。在你的情况下它应该工作得很好,因为你的 tableview 不是很大。只需在 cell == nil 检查之前添加以下行:

cell = nil;

它现在应该可以工作了。

于 2012-06-26T12:51:49.703 回答
0

我有同样的问题,避免它的一种方法是在下载过程中锁定表格的滚动能力。

于 2013-03-27T13:31:06.930 回答