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中删除
    }
}
相关文章
相关标签/搜索