UITrogressbar在UITableViewCell问题中

时间:2012-06-17 20:37:41

标签: iphone uitableview asihttprequest

在我的应用程序中,我有一个tableview。每个单元格包含一个UIButton,UIImageView和一个UILabel。当点击按钮时,将使用ASINetworkQueue下载一组文件,此时该按钮将被UIProgressView替换。下载进度时,如果我滚动表格,应用程序崩溃。我在ASIHTTPRequest.m类中得到了错误点,

+ (void)updateProgressIndicator:(id *)indicator withProgress:(unsigned long long)progress ofTotal:(unsigned long long)total
{
    #if TARGET_OS_IPHONE
        // Cocoa Touch: UIProgressView
        SEL selector = @selector(setProgress:);
        float progressAmount = (float)((progress*1.0)/(total*1.0));

    #else
        // Cocoa: NSProgressIndicator
        double progressAmount = progressAmount = (progress*1.0)/(total*1.0);
        SEL selector = @selector(setDoubleValue:);
    #endif

    if (![*indicator respondsToSelector:selector]) { // here i am getting the error 
        return;
    }

    [progressLock lock];
    [ASIHTTPRequest performSelector:selector onTarget:indicator withObject:nil amount:&progressAmount callerToRetain:nil];
    [progressLock unlock];
}

这是我的代码: 我编写了一个UITableViewCell类,如

@interface UIMenuItemCell : UITableViewCell{
    UILabel *cellItemName;
    UIImageView *cellitemImage;
    UIButton *cellItemButton;
    UIProgressView *cellItemProgress;
}
@property (nonatomic, retain) UILabel *cellItemName;
@property (nonatomic, retain) UIImageView *cellitemImage;
@property (nonatomic, retain) UIButton *cellItemButton;
@property (nonatomic, retain) UIProgressView *cellItemProgress;

- (UIMenuItemCell *) getCellContentView:(NSString *)cellIdentifier {

    CGRect CellFrame = CGRectMake(0, 0, 150, 60);
    CGRect imgFrame = CGRectMake(20, 48, 110, 123);
    CGRect btnFrame = CGRectMake(25, 140, 100, 26);

    UIImageView *itemImg;
    UIButton *itemBtn;

    UIMenuItemCell *cell = [[UIMenuItemCell alloc] init] ;
    cell.frame = CellFrame;

    //Initialize ImageView
    itemImg = [[UIImageView alloc]initWithFrame:imgFrame];
    itemImg.tag = 2;
    [cell.contentView addSubview:itemImg];

    //Initialize Button
    itemBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    itemBtn.frame = btnFrame;
    itemBtn.tag = 3;
    itemBtn.titleLabel.textColor = [UIColor blueColor];
    itemBtn.titleLabel.font = [UIFont systemFontOfSize:9.0];
    [cell.contentView addSubview:itemBtn];

    return cell;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UIMenuItemCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)
        cell = [self getCellContentView:CellIdentifier];

    cell.cellitemImage = (UIImageView *)[cell viewWithTag:2];
    cell.cellItemButton = (UIButton *)[cell viewWithTag:3];

    DataBaseClass *itemObj = [appDelegate.itemArray objectAtIndex:indexPath.row];
    NSString *url;
    if ([itemObj.itemStatus isEqualToString:@"NotAvailable"]) {
        url = [NSString stringWithFormat:@"%@",itemObj.notAvialableIcon];
        [cell.cellItemButton setTitle:date forState:UIControlStateNormal]; 
        cell.cellItemButton.userInteractionEnabled = NO;
        cell.userInteractionEnabled = NO;
        [cell.cellItemButton setBackgroundImage:[UIImage imageNamed:@"not_available_bttn_bck_img"] forState:UIControlStateNormal];
    }else if([itemObj.itemStatus isEqualToString:@"Available"]){
        cell.cellItemButton.userInteractionEnabled = YES;
        cell.userInteractionEnabled = YES;
        [cell.cellItemButton setTitle:@"" forState:UIControlStateNormal];
        [cell.cellItemButton setBackgroundImage:[UIImage imageNamed:@"img_normal"] forState:UIControlStateNormal];
        [cell.cellItemButton setBackgroundImage:[UIImage imageNamed:@"img_pressed"] forState:UIControlStateHighlighted];
        [cell.cellItemButton addTarget:self action:@selector(download) forControlEvents:UIControlEventTouchUpInside];
        url = [NSString stringWithFormat:@"%@",itemObj.availableIcon];
    }

    [cell.cellitemImage setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"item01.png"]];

    cell.cellItemName.text = [NSString stringWithFormat:@"%@",itemObj.itemName];

    return cell;
}

和下载操作:

- (void)download{
//adding custom method to add the uiprogressview into the selected cell
//and setting the progress delegate for the queue
[self.myQueue setDownloadProgressDelegate:currentProgress];
//then starts download
[self.myQueue go];
}

请分享您的想法。 编辑:我在这里同时实现了多次下载。

2 个答案:

答案 0 :(得分:1)

当您的单元格离开屏幕时,进度条会被释放,ASI将尝试更新已发布的对象,您需要做的是将两者相互分离,您可以创建一个下载程序类并且让这个班级向小区发送通知,请注意这需要你身边的一些工作,祝你好运:)

答案 1 :(得分:1)

@Omar对这个问题是正确的。这是解决方案的草图:

将类似“downloadProgress”的属性添加到DatabaseClass。这可能是一个浮点数,当没有下载时,它的值为负值,而下载时则为0.0到1.0之间。

向自定义单元格添加隐藏的进度指示器。

按下下载按钮时,获取相应的数据库项并开始下载(将项目的downloadStatus设置为0.0)。当您从异步流程获得进展时,请更新该项目的downloadStatus。

每次执行此操作时,请让表知道状态已更改(NSNotification可能是一个很好的机制)。表VC应该使用包含与数据库项对应的索引路径的数组调用reloadRowsAtIndexPaths:。

配置单元格时,表格VC应检查项目的下载状态。如果是正数,则取消隐藏状态指示器并根据浮动设置它的进度。

另外,关于崩溃:看起来在updateProgress方法中至少还有一个问题,其中指示符是键入的(id *)。这就像一个指向id的指针,几乎肯定不是你想要的。只需使用id,不要将正文中的指标称为*指标。