GroupedスタイルのUITableViewのfooterを右寄せ/左寄せにする
GroupedスタイルのUITableViewのfooterテキストを左寄せにしたかったのですが、結構大変でしたのでライブラリにしてgithubに置きました。GFGroupedTableViewAdditionsです。
使い方
1. ViewControllerのプロパティにGFGroupedTableFooterViewを追加します。
#import "UITableView+GFGroupedTableViewAdditions.h" @interface MyViewController <UITableViewDelegate> // ... @property (weak, nonatomic) IBOutlet UITableView *tableView; // From NIB @property (strong, nonatomic) GFGroupedTableFooterView *footer1; // Footer for first section @end
2. ViewDidLoadでGFGroupedTableFooterViewを初期化します。UITableVIewにgroupedFooterViewWithText:textAlignment:メソッドが追加されていますのでそれを使ってください。テキストからfooterの適切なサイズを計算します。
@implementation MyViewController - (void)viewDidLoad { [super viewDidLoad]; // ... // Initialize GFGroupedTableFooterView _footer1 = [_tableView groupedFooterViewWithText:@"FOOTER TEXT" textAlignment:UITextAlignmentLeft]; } // ...
3. tableView:heightForFooterInSection:ではGFGroupedTableFooterViewのheightメソッドの値を、tableView:viewForFooterInSection:ではGFGroupedTableFooterViewを返すように変更します。tableView:titleForFooterInSection:を既に実装している場合は消してください。
@implementation MyViewController // ... - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { // Returns valid height for first section if (section == 0) return [_footer1 height]; return 0.0f; } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { // Returns footer view for first section if (section == 0) return _footer1; return 0.0f; }
もうちょい簡単に実装できる方法もあるような気がしますが分かりませんでしたのでとりあえずこの形で。