簡體   English   中英

在運行時調整UITableViewCell內部的UIView的寬度

[英]Adjusting the Width of UIView Inside UITableViewCell at Runtime

我有一個帶有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);

}

剩余的BudgetView是Storyboard中原型單元內的UIView。 但是以上內容永遠不會更改剩余預算視圖的寬度和高度。 我究竟做錯了什么?

您的IBOutlets是否已連接? 忘記了,即使我問了:P。

不會,因為從tableview出隊的tableviewcell(未使用initWithSomething實例化)在准備好繪制時會調用默認的layoutsubview。 因此,即使您設置了框架,layoutsubviews也將覆蓋您的框架更改,並將其設置為在情節提要的原型單元中設置為視圖的默認框架。

出路可以是重新思考問題。 也許你想你的預算視圖是一個UILabel或一組UILabels 您可以為它們分配全角,並根據需要將文本左對齊或居中對齊。 我希望我清楚。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM