9/14/2011

UITableViewのtableFooterViewにUIButtonを追加する

参考画像

このように追加でデータを読み込むような使い方をするケースが多い。
テーブルを引っ張って更新するのが流行りだが、Footerの場合はCellの数によって
トリガーとなる基準の位置座標が変わるので、UIButtonで実装する方が簡単。

- (void)viewDidLoad{}内でUIButtonを作成してtableViewFooterに設定する。
- (void)viewDidLoad
{
    [super viewDidLoad];
readMoreButton = [UIButton buttonWithType:UIButtonTypeCustom];
[readMoreButton addTarget:self action:@selector(readMoreButtonPushed) forControlEvents:UIControlEventTouchUpInside];
[readMoreButton setTitle:@"Read more" forState:UIControlStateNormal];
[readMoreButton setTitle:@"Load" forState:UIControlStateHighlighted];
[readMoreButton setTitle:@"Loading" forState:UIControlStateDisabled];
readMoreButton.backgroundColor = LIGHT_BACKGROUND;
readMoreButton.frame = CGRectMake(0, 0, self.tableView.frame.size.width, 44);
self.tableView.tableFooterView = readMoreButton;
}

- (void)readMoreButtonPushed{
readMoreButton.enabled = NO;
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"message" message:@"Read more button pushed." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
readMoreButton.enabled = YES;
}


上の実装例は設定したボタンを押すとアラートがポップアップする。
実際にはデータを読み込む処理や、読み込んだ後に追加されたデータの直前まで
テーブルをスクロールさせるような処理を追加する。

0 件のコメント:

コメントを投稿