2014-10-28 62 views
0
- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
               reuseIdentifier:@"UITableViewCell"]; 

    //set the cell's text.... 

    UIProgressView *prg =[[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault]; 
    prg.tag = indexPath.row + 1000; 
    prg.frame = CGRectMake(0, (cell.frame.size.height - prg.frame.size.height), cell.frame.size.width, 10); 

    [prg setTintColor:[UIColor colorWithRed:68/255.0 green:183/255.0 blue:104/255.0 alpha:1.0]]; 
    [cell addSubview:prg]; 

    return cell; 
} 

這適用於iPhone - 進度視圖與單元寬度相同,但在iPad上(這是通用應用程序),它只有單元的一半寬。我該如何解決?如果您使用自動佈局你最好儘量避免幀儘可能如何在與單元寬度相同的UITableViewCell中插入UIProgressView?

prg.autoresizingMask = UIViewAutoresizingFlexibleWidth; 

回答

1

+0

我在嘗試時遇到這種情況:無法解析約束格式:無法解釋'|'字符,因爲相關視圖沒有超級視圖 | [progressView] | – croceldon 2014-10-28 19:22:25

+0

@croceldon在添加約束之前,您必須添加子視圖。 [cell.contentView addSubview:prg]; – BooRanger 2014-10-29 08:21:00

0

讓它自動調整。

的進步視圖添加到細胞你要沿着線的東西:

// Edit: add the view to the superview first 
[cell.contentView addSubview:prg]; 

prg.translatesAutoresizingMaskIntoConstraints = NO; 

// set width to match the superviews width 
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[progressView]|" options:kNilOptions metrics:nil views:@{@"progressView":prg}]]; 

// you don't need to set the height for a UIProgressView, it will default to 2pts I think 

你會希望把這個地方將只調用一次,你也只需要創建進度視圖一旦當前顯示單元格時創建progressView,如果該單元格被重用X progressView將被創建的次數X次數。 。

+0

我使用自動佈局,這仍然可以與約束嗎? – croceldon 2014-10-28 16:53:16

+0

它應該如果你添加'prg.translatesAutoresizingMaskIntoConstraints = YES'。或者,'[cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@「H:| [prg] |」 options:0 metrics:nil views:NSDictionaryOfVariableBindings(prg)]];''可以做到這一點。 – 2014-10-28 16:57:07

相關問題