在运行时调整UITableViewCell内UIView的宽度

时间:2014-07-03 20:07:03

标签: ios

我有一个带有cellForIndexPath方法的UITableViewController,如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BudgetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BudgetTableViewCell" forIndexPath:indexPath];

    Budget *budget = [_budgets objectAtIndex:[indexPath row]];

    [cell configure:budget];

    return cell;
}



  @interface BudgetTableViewCell : UITableViewCell
    {

    }

    @property (nonatomic,weak) IBOutlet UILabel *titleLabel;
    @property (nonatomic,weak) IBOutlet UIView *remainingBudgetView;

在单元格的configure方法中,我执行以下操作:

-(void) configure:(Budget *)budget
{
    self.remainingBudgetView.frame = CGRectMake(0, 0, 400, 300);

}

在Storyboard的原型单元格中,remainingBudgetView是UIView。但是上面的内容永远不会改变remainingBudgetView的宽度和高度。我做错了什么?

1 个答案:

答案 0 :(得分:2)

您的IBOutlets是否已关联?忘了,即使我问过:P。

它不会,因为tableviewcells从tableview中出列(并且没有使用initWithSomething实例化)在准备好绘制时会调用默认的layoutsubviews。因此,即使您设置了框架,layoutsubviews也会覆盖您的框架更改并将其设置为您在storyboard中的原型单元格中设置为视图的默认框架。

出路可以是重新思考问题。您可能希望预算视图为UILabel或一组UILabels。您可以根据需要为它们分配全宽并将文本对齐到左侧或中间。我希望我清楚。