2016-10-16 5 views
1

私のアプリケーションでは、セルを選択すると、動的にテーブルビューのフッターテキストを更新しています。残念ながら私のやり方はUILabelのフレームを私のUIViewに変更するようです。たとえば、セルをタップすると、ラベルの幅は950と1200の間になります。実際には幅は(最大で)ビューの幅にする必要があります。ここに私のコードは次のとおりです。UITableViewフッターでラベルを動的に設定およびサイズ変更

def tableView(tableView, viewForFooterInSection:section) 
    (section != (self.sections.count - 1)) ? footer_view : nil 
end 

def tableView(tableView, heightForFooterInSection: section) 
    if section != (self.sections.count - 1) 
    footer_label.frame.size.height + 20.0 
    else 
    1.0 
    end 
end 

def tableView(tableView, didSelectRowAtIndexPath: indexPath) 
    tableView.beginUpdates 
    footer_label.text = "Details: #{updated_details}" 
    footer_label.sizeToFit 
    tableView.endUpdates 
end 


def footer_view 
    @footer_view ||= UIView.alloc.initWithFrame(CGRectZero).tap do |fv| 
    fv.addSubview(footer_label) 
    end 
end 

def footer_label 
    @footer_label ||= UILabel.alloc.initWithFrame(CGRectZero).tap do |pl| 
    pl.frame = CGRect.new(
     [15, 10], 
     [self.view.frame.size.width - 30, self.view.frame.size.height] 
    ) 
    pl.font = UIFont.preferredFontForTextStyle(UIFontTextStyleCaption2) 
    pl.numberOfLines = 0 
    pl.lineBreakMode = NSLineBreakByWordWrapping 
    pl.text   = "" 
    pl.sizeToFit 
    end 
end 

私はsizeToFitを呼び出す推測している問題の一部ですか?

+0

は、[この1](http://stackoverflow.com/questions/39825290/make-uicollectionview-に似てあなたのケースですheader-dynamic-height/39832379#39832379)? –

答えて

0

フッターの動的な高さを使用する必要があると思います。

tableView.heightForFooterInSection = UITableViewAutomaticDimension 
tableView.estimatedSectionFooterHeight = 25.0 

UILabelの制約を設定する必要があります。 セクションを更新して高さを変更する必要があるかもしれません。

1

私はこれを試していたと思ったが、おそらく私の注文は中止していた。いずれにせよ、私はbeginUpdates/endUpdatesの内側に再びフッターの枠を設定することによってそれを解決:

tableView.beginUpdates 
frame = footer_label.frame 
footer_label.text = "Plan Details: #{updated_details}" 
frame.size.width = self.view.frame.size.width - 30 
footer_label.frame = frame 
footer_label.sizeToFit 
tableView.endUpdates 
関連する問題