问题
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