UICollectionView与重用单元格和滚动有关

时间:2014-12-10 07:17:50

标签: ios objective-c ios8 uicollectionview

我正在将自定义单元格加载到 UICollectionView ,并且在每个 CELL1 上我有一个下载按钮和进度视图,但是当我开始时点击cell1按钮后下载的东西。我开始下载了我正在更新进度视图,显示正确的结果。但是当进度视图开启时(正在进行下载),当我向下滚动时,我看到 CELL5上的进度视图,再次如果我通过上下滚动来玩它我能够在 CELL6 看到进度视图......我在这里做错了什么?...

我的代码在这里:

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath

{
    MyCell *cell = (MyCell*)[cv dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];


    cell.labelTitle.text = [_editionNameArray objectAtIndex:indexPath.row];

    cell.labelIssue.text = [[[_editionDescriptionArray objectAtIndex:indexPath.row]
                             stringByReplacingOccurrencesOfString:@"+" withString:@" "]
                            stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    cell.issueButton.tag=indexPath.row;

    [cell.issueButton setTitle:@"Download" forState:UIControlStateNormal];//once the download is completed set the title as Read.
      [cell.issueButton addTarget:self action:@selector(downloadTheIssue:) forControlEvents:UIControlEventTouchUpInside];

    cell.tag=indexPath.row;
}

收到数据后,我正在更新进度视图,

- (void)downloadManager:(DownloadManager *)downloadManager downloadDidReceiveData:(Download *)download
{

 [self updateProgressViewForIndexPath:[NSIndexPath indexPathForRow:download.tag inSection:0] download:download];

}

- (void)updateProgressViewForIndexPath:(NSIndexPath *)indexPath download:(Download *)download
{

    MyCell *cell = (MyCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
    // if the cell is not visible, we can return

    if (!cell)
        return;
    cell.progressView.hidden=NO;
    if (download.expectedContentLength >= 0)
    {
        // if the server was able to tell us the length of the file, then update progress view appropriately
        // to reflect what % of the file has been downloaded

        cell.progressView.progress = (double) download.progressContentLength / (double) download.expectedContentLength;
    }
    else
    {
        // if the server was unable to tell us the length of the file, we'll change the progress view, but
        // it will just spin around and around, not really telling us the progress of the complete download,
        // but at least we get some progress update as bytes are downloaded.
        //
        // This progress view will just be what % of the current megabyte has been downloaded

        cell.progressView.progress = (double) (download.progressContentLength % 1000000L) / 1000000.0;
    }
}

更新

#import "MyCell.h"

@implementation MyCell


- (void)layoutSubviews {
    [super layoutSubviews];
    self.backgroundView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"gridbg.png"]];
    self.labelTitle.textColor = [UIColor colorWithRed:0.18f green:0.20f blue:0.23f alpha:1.00f];
    self.labelTitle.backgroundColor = [UIColor clearColor];

    self.labelIssue.textColor = [UIColor colorWithRed:1.00f green:0.25f blue:0.55f alpha:1.00f];
    self.labelIssue.backgroundColor = [UIColor clearColor];

}

非常感谢任何解决方案......

2 个答案:

答案 0 :(得分:2)

实际上你应该在某个地方存储每个项目的进度,因为当重用单元格时,它将会丢失并且永远不会恢复。例如,为此设置NSMutableArray *_downloadProgressArray并相应地调整您的方法:

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyCell *cell = (MyCell*)[cv dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];

    cell.labelTitle.text = [_editionNameArray objectAtIndex:indexPath.row];
    cell.labelIssue.text = [[[_editionDescriptionArray objectAtIndex:indexPath.row]
                         stringByReplacingOccurrencesOfString:@"+" withString:@" "]
                        stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSNumber* progress = [_downloadProgressArray objectAtIndex:indexPath.row];
    if (progress == nil) {
        // download not started
        cell.issueButton.hidden = NO;
        cell.issueButton.tag = indexPath.row;
        [cell.issueButton setTitle:@"Download" forState:UIControlStateNormal]; //once the download is completed set the title as Read.
        [cell.issueButton addTarget:self action:@selector(downloadTheIssue:) forControlEvents:UIControlEventTouchUpInside];
        cell.progressView.hidden = YES;
    } else {
        cell.issueButton.hidden = YES;
        cell.progressView.hidden = NO;
        cell.progressView.progress = progress.doubleValue;
    }

    cell.tag = indexPath.row;
    return cell;
}


- (void)downloadManager:(DownloadManager *)downloadManager downloadDidReceiveData:(Download *)download
{
    _downloadProgressArray[download.tag] = [NSNumber numberWithDouble:(your progress here)];
    [self updateProgressViewForIndexPath:[NSIndexPath indexPathForRow:download.tag inSection:0] download:download];
}

下载完成后不要忘记设置nil

答案 1 :(得分:0)

如您所说,您正在重复使用单元格,然后您必须将值重置为默认值,因为进度条值已更改,并且单元格在其他位置重复使用。

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
MyCell *cell = (MyCell*)[cv dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
cell.progressView.progress = 0;
....

其他标签正在获取更新的值cell.labelIssue.text,因此除了progressView值之外没问题。