Table

顯示出來的是UITableView(是UIView的子類),控制它的是UITableViewController。表格的每一行都是UITableViewCell類。數組

 

 

想要建立一個類負責顯示出一個表格,就把這個類建爲UITableViewController的子類。它會負責3個內容:函數

(1)控制UITableView在屏幕上的樣子。spa

(2)充當表格的data source。因此要實現UITableViewDataSource Protocol。code

(3)充當表格的delegate。因此要實現UITableViewDelegate Protocol。orm

 

建立表格頁面的步驟:對象

STEP 1:新建一個UITableViewController的子類。初始化時用initWithStyle:。有兩個選擇:UITableViewStylePlainUITableViewStyleGrouped。或者也可向下方代碼所示:blog

//-------------UITableViewController類的默認初始化方法爲initWithStyle{ },爲了防止有人直接調用init{ }進行初始化,乾脆直接寫一個init{ },並在initWithStyle{ }中轉到init{ }裏。
- (id)init{
    self = [super initWithStyle:UITableViewStyleGrouped];return self;
}

- (id)initWithStyle:(UITableViewStyle)style
{
    return [self init];
}

STEP 2:讓這個類能作表格的data source,即實現UITableViewDataSource接口中的兩個函數tableView:numberOfRowsInSection:和tableView:cellForRowAtIndexPath接口

//---------要想使某一個類完成UITableViewController的工做、即變成DataSource,就要實現UITableViewDataSource Protocol中的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[[BNRItemStore defaultStore] allItems] count];//該方法用來讓tableView得到數據源究竟有幾行,即在tableView中須要顯示幾行。
}
//-----存在一個複用tableViewCell的問題 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];//首先檢查是否有能夠重用的單元格,若是存在就使用
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
    }
    BNRItem *p = [[[BNRItemStore defaultStore]allItems]objectAtIndex:[indexPath row]];//把defaultStore數組中的第row個取出來
    [[cell textLabel]setText:[p description]];//用取出的節點的description設置單元格cell的文本
        
    return cell;
}

 

STEP 3:若是想要點擊表格的某一行就發生點什麼事,就再tableView:didSelectRowAtIndexPath:中寫。ip

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ //完成功能:點擊哪一行,就跳出後續頁面detViewController,並在上面顯示這一行的內容
    
    DetViewController *dvc = [[DetViewController alloc]init];//生成要顯示的後續頁面
    
    NSArray *items = [[BNRItemStore defaultStore]allItems];//defaultStore已在BNRItemStore.m中被定義爲static類型,因此實際上不管didSelectRowAtIndexPath函數被調用幾回,defaultStore都只有一個。把defaultStore中得全部對象都取出來放在items中。
    BNRItem *selectedItem = [items objectAtIndex:[indexPath row]];
    [dvc setBnritem:selectedItem];
    
    [[self navigationController] pushViewController:dvc animated:YES]; //把dvc壓到navigationController棧的頂部,即顯示出來
    
}

STEP 4:在表格中增長新的行:insertRowsAtIndexPaths:withRowAnimation:rem

- (IBAction)addNewItem:(id)sender{
    
    BNRItem *newItem = [[BNRItemStore defaultStore] createItem];//在View上插入以前先生成一個新的BNRItem結點
    int lastRow = [[[BNRItemStore defaultStore]allItems]indexOfObject:newItem];
    NSIndexPath *ip = [NSIndexPath indexPathForItem:lastRow inSection:0];
    
    [[self tableView]insertRowsAtIndexPaths:[NSArray arrayWithObject:ip] withRowAnimation:UITableViewRowAnimationTop];//把新行插入table
    
}

STEP 5:讓表格變得可被編輯:

- (IBAction)toggleEdittingMode:(id)sender{
    if ([self isEditing]) {
        [sender setTitle:@"Edit" forState:UIControlStateNormal];
        [self setEditing:NO animated:YES];
    }else{
        [sender setTitle:@"Editing" forState:UIControlStateNormal];
        [self setEditing:YES animated:YES];
    }
}

STEP 6:刪除表格中得某一行:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {//若是表格視圖提交的是刪除命令。。。
        BNRItemStore *ps = [BNRItemStore defaultStore];
        NSArray *items = [ps allItems];
        BNRItem *p = [items objectAtIndex:[indexPath row]];
        [ps removeItem:p];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];//把這一行也從table view中刪除
    }
}
相關文章
相關標籤/搜索