首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIProgressbar in UITableViewCell问题

UIProgressbar in UITableViewCell问题
EN

Stack Overflow用户
提问于 2012-06-17 20:37:40
回答 2查看 636关注 0票数 0

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

代码语言:javascript
复制
+ (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类,

代码语言:javascript
复制
@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;

代码语言:javascript
复制
- (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;
}

以及下载操作:

代码语言:javascript
复制
- (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];
}

请分享你的想法。编辑:,我在这里同时实现多个下载。

EN

回答 2

Stack Overflow用户

发布于 2012-06-17 20:41:31

正在发生的情况是,当您的单元格离开屏幕时,进度条被释放,ASI将尝试更新一个已释放的对象,您需要做的是将两者分离开来,您可以创建一个下载器类,并让该类向单元格发送通知,请注意,这将需要在您身边做一些工作,祝您好运:)

票数 1
EN

Stack Overflow用户

发布于 2012-06-17 21:51:28

@Omar关于这个问题是正确的。下面是一个解决方案的草图:

将类似于"downloadProgress“的属性添加到DatabaseClass中。这可以是一个浮点,当没有下载时,它的值是负值,而在下载发生时,它的值在0.0到1.0之间。

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

按下“下载”按钮时,获取相应的数据库项并启动下载(将项目的downloadStatus设置为0.0)。当您从异步过程中获得进展时,更新该项的downloadStatus。

每次这样做,都要让表知道状态发生了变化(NSNotification可能是一种很好的机制)。表VC应该使用包含与数据库项对应的索引路径的数组调用reloadRowsAtIndexPaths:。

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11074605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档