2014年7月10日 星期四

tableView

重新載入資料

全部
[self.tableView reloadData];
更新特定row
[self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
別人的文章:UITableViewController 之 dataSource 更新顯示


不知道什麼時候要加@@
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];


設定接下來要顯示的viewController的title,有兩種方式:

prepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UIViewController *page2 = segue.destinationViewController;
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    (indexPath.row == 0) ? [page2 setTitle:@"Go To Bed Time"] : [page2 setTitle:@"Wake Up Time"];
}

didSelectRowAtIndexPath,需要另外設定目標viewController的Storyboard ID。但如果需要隔空轉換畫面,也就是不用segue轉換畫面的話,比如:同一個button一不同的情況要轉換到不同的view:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController *page2 = [self.storyboard instantiateViewControllerWithIdentifier:@"page3"];
    (indexPath.row == 0) ? [page2 setTitle:@"Go To Bed Time"] : [page2 setTitle:@"Wake Up Time"];
    [self.navigationController pushViewController:page2 animated:YES];
}

如果轉換模式要用modal
[self presentViewController:page2 animated:YES completion:nil];
completion那裡可以放轉換過去後要執行的程式

indexPath

The NSIndexPath class represents the path to a specific node in a tree of nested array collections.
A table has sections and rows, to know where a row is you need to know the index of which section it is in and the index position it is at within that section.
UIKit adds a category to make it easier to work with UITableView's.
@interface NSIndexPath (UITableView)

+ (NSIndexPath *)indexPathForRow:(NSInteger)row inSection:(NSInteger)section;

@property(nonatomic,readonly) NSInteger section;
@property(nonatomic,readonly) NSInteger row;

@end
資料來源:http://stackoverflow.com/a/10807882/3295047