UITableViewCell as progress indicator

南笙酒味 提交于 2020-01-14 06:12:07

问题


I am trying to set the whole cell background view to show a progress indicator.

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

this does not set the progress bar to the whole cell, but just the top part of the cell. Should I make the cell transparent some how ? How can I use the whole cell background to show progress with a color fill, while text on the cell remain visible ?


回答1:


You need to set the height of your UIProgressView and add it as a content subview for you cell, like this:

- (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;
}



回答2:


I think other than progress View if you want you can use this in 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;


来源:https://stackoverflow.com/questions/25461281/uitableviewcell-as-progress-indicator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!