UITableViewCell中的UIProgressbar问题 [英] UIProgressbar in UITableViewCell issue

查看:60
本文介绍了UITableViewCell中的UIProgressbar问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my application, i have a tableview. And each cell contains, a UIButton, UIImageView and a UILabel. And when click on the button, a group of files will be downloaded using ASINetworkQueue, at this time the button will be replaced with UIProgressView. While the download progress, if I scroll the table, the app crashes. And I am getting the error point in ASIHTTPRequest.m class,

+ (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类,

And here is my code: I have written a UITableViewCell class like,

@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];
}

请分享您的想法. 在这里,我要同时实现多个下载.

Please share your thoughts. Here I am implementing multiple downloads at the same time.

推荐答案

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

@Omar is correct about the problem. Here's a sketch of a solution:

向DatabaseClass添加"downloadProgress"之类的属性.这可以是一个浮动对象,当没有下载发生时,其值为负;而在下载发生时,其值为0.0至1.0.

Add a property like "downloadProgress" to DatabaseClass. This can be a float who's value is negative when no download is happening and between 0.0 and 1.0 while download is occurring.

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

Add a hidden progress indicator to your custom cell.

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

When download button is pressed, get the corresponding database item and start the download (setting the item's downloadStatus to 0.0). As you get progress from the asynch process, update that item's downloadStatus.

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

Each time you do that, let the table know that status is changed (NSNotification might be a good mechanism here). The table VC should call reloadRowsAtIndexPaths: with an array containing the index path corresponding to the database item.

在配置单元格时,表VC应该检查该项的下载状态.如果为正,请取消隐藏状态指示器,并根据浮动控件设置其进度.

When configuring the cell, the table VC should check the item's download status. If it's positive, unhide the status indicator and set it's progress according to the float.

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

Also, about the crash: it looks like there's at least one other problem in the updateProgress method where the indicator is typed (id *). This is like a pointer to an id, and almost certainly not what you want. Just use id, and don't refer to indicator in the body as *indicator.

这篇关于UITableViewCell中的UIProgressbar问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆