CodeWeed CodeWeed - 5 months ago 58
iOS Question

UITableViewCell as progress indicator

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 ?

Answer

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