UITableViewCell作为进度指示器

时间:2014-08-23 11:10:16

标签: ios objective-c cocoa-touch uitableview uiprogressview

我正在尝试设置整个单元格背景视图以显示进度指示器。

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
    cell.backgroundView = progressView;
    [progressView setFrame:cell.bounds];
}

这不会将进度条设置为整个单元格,而只是单元格的顶部。我应该让细胞透明吗? 如何使用整个单元格背景显示颜色填充的进度,而单元格上的文本仍然可见?

2 个答案:

答案 0 :(得分:1)

我认为除了进展以外,您可以在cellForRowAtIndexPath:

中使用此功能
cell.backgroundColor = [UIColor clearColor];
CGFloat fillWidth = (75.0/100.0) * cell.frame.size.width;

CGRect rect = CGRectMake(0, 0, fillWidth, cell.frame.size.height);
UIView * view = [[UIView alloc] initWithFrame:rect];
view.backgroundColor = [UIColor purpleColor];
[cell.contentView addSubview:view];

//...

return cell;

答案 1 :(得分:1)

您需要设置UIProgressView的高度并将其添加为您的单元格的内容子视图,如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
        UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
        //setting height of progressView
        CGAffineTransform transform = CGAffineTransformMakeScale(cell.frame.size.width, cell.frame.size.height);
        progressView.transform = transform;
        [cell.contentView addSubview:progressView];

        //your text
        cell.textLabel.text = @"Cell Text Label";
        cell.detailTextLabel.text = @"Cell Detail Label";
    }

    return cell;
}