单击tableview单元格中的下载按钮时,在相应单元格中显示进度视图

时间:2012-06-26 12:39:44

标签: iphone

我是iphone的新手。我在某个任务(即)中遇到了我的项目,我有一个66行的表格视图。我为每个单元格放置了不同的书名,并为每本书放置了一个下载按钮我的要求就是当我们点击下载按钮时,它只显示该特定单元格中的进度视图,但是我进入了那个特定的单元格但是当我拖动tableview时它会在某些单元格中显示进度视图。这是因为dequeue可重用性概念,但我不知道如何避免这个问题。我想甚至在拖动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];

}
我的项目的

screen shot是[我的上述代码输出在模拟器中]

2 个答案:

答案 0 :(得分:0)

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

cell = nil;

现在应该可以了。

答案 1 :(得分:0)

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