2012-06-26 22 views
1

我是iphone的新手。我在某些任务(ie)中敲我的项目,我有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]; 

} 

我的项目screen shot为[我上面的代码输出,在模拟器]您应该为零细胞在cellForRow每次

回答

0

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

cell = nil; 

它现在应该工作。

0

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

相关问题