UITableView 的浅显理解

      在UIKit.framework 中的 UITableView 被限制成只能竖着显示,因为它被设计在比较小屏幕的设备上运行。UITableView 是UIScrollView的子类,但是它只能允许卷动。 UITableViewCell 是 UITableView中每项的元素。Table View 是可以被用户操作的(删除子类的)。

    Sections指的是Table View 中有多少项,而Row 是指每个Section 又多少项。

    Table views 有 UITableViewStylePlain 和 UITableViewStyleGrouped 两种风格。创建Table View后必须要定义好 风格,并且不可以再改变。plain 类型中,假如 section 的头和脚是可见的将会在内容的上面 ,在Table VIew的右边有每项的索引(如 a - z)。grouped 类型没有索引,它有默认的背景颜色和背景图片,背景图片可以为特定的 section 中所有的 cell 提供可视化分组。

    很多方法使用  NSIndexPath 作为参数和返回值。

     UITableView一定要有一个扮演 data source 和一个扮演 delegate 角色的object。通常这些objects要么是application delegate ,或者更多的是 一个 custom UITableView。data source 一定要继承 UITableViewDataSource ,delegate 一定要继承 UITableViewDelegate。data source 提供给 UITableView 构建表格和当需要对表进行操作的数据模型所需要的数据。delegate 提供cell 给 table 和在其他任务上需要的。

    setEditing:animated: message (setEditing参数是 YES) 方法让表格进入编辑模式。点击插入或者删除控制器,将会使 data source 接受 tableView:commitEditingStyle:forRowAtIndexPath: message 方法。通过 deleteRowsAtIndexPaths:withRowAnimation: 或者 insertRowsAtIndexPaths:withRowAnimation 来完成插入或者删除。在编辑模式中,如果 table-view cell 有 将showsReorderControl属性设置为YES,data source 将会接受 tableView:moveRowAtIndexPath:toIndexPath: message。data source 通过实现 tableView:canMoveRowAtIndexPath:.能够为cell 选择性删除重新排序器。
    

    首先,假如你想要使用UITableView,你需要继承 UITableViewDataSource, UITableViewDelegate。

    如果你使用xib文件去构建界面,UIView 和 UITableView 的大小一定要适配,我第一次用不执行委托的方法就是因为大小不适配不执行(可能系统认为你的TableView不会显示出来)。

   实现一下四个方法


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView // 多少个section,在这里我只需要一个。
{
   
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section// 每个section 有多少个cell
{
    // Return the number of rows in the section.
    NSInteger cellNum= self.caseInfo.count;
    return cellNum;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{// 一个cell的大小(高度)//函数3
    return 160;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //构建每个单元//函数4
    
    static NSString *CaseProgressInfoPlusCellIdentifier = @"CaseProgressInfoPlusCell";
    NSInteger objIndex = indexPath.row;
    CaseCarInfo *tempCaseInfo = [self.caseInfo objectAtIndex:objIndex];
    
    CaseProgressInfoPlusCell *cell = (CaseProgressInfoPlusCell *)[tableView dequeueReusableCellWithIdentifier:CaseProgressInfoPlusCellIdentifier];
        if(nil == cell){
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CaseProgressInfoPlusCell" owner:self options:nil];
            
            for (id oneObj in nib) {
                if([oneObj isKindOfClass:[CaseProgressInfoPlusCell class]]){
                    cell = (CaseProgressInfoPlusCell *)oneObj;
                }
            }
            cell.carNOLabel.text = [[NSString alloc] initWithFormat:@"%@  (%@)", tempCaseInfo.car_mark,tempCaseInfo.first_third];
            cell.missionStateLabel.text = tempCaseInfo.task_status;
            cell.checkDamageMoneyLabel.text = tempCaseInfo.estimate_amount;
        }
        return cell;        
}


函数1,2,3可以看做是对表格初始化,不包括表格的内容,函数4对每个单元进行初始化。执行的顺序是 函数1 确定有多少section,然后执行函数2确定有多少个cell,确定每一项有多少小项。函数2的执行次数会根据函数1返回的值来确定。然后执行函数3,函数3的执行的次数根据函数2的返回值来确定。然后执行函数4,函数四执行的次数也根据函数2来确定。

      函数1 ---> 函数2-->函数3--->函数四。一级一级的循环。

      还有很多的方法,见API文档。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值