UITableView-section與row

確定section與row代碼:

//幾個section
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
    return 6;
}
//每個section包含的row
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    switch(section) {
        case0:
            return 1;
            break;
        case1:
            return 1;
            break;
        case2:
            return 2;
            break;
        case3:
            return 1;
            break;
        case4:
            return 1;
            break;
        case5:
            return 1;
            break;
        default:
            return 0;
            break;
            }
}

之后在數(shù)據(jù)源方法中確認每個section下cell的identifier

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
switch(indexPath.section) {
case0:
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
return cell;
break;
}
...
return nil;
}

下面確認每個section的title,高度與row的高度

//title
- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{
    NSString* sectionName;
    switch(section){
        case 0:
            sectionName = self.titleArray[0];
            break;
        case 1:
            sectionName = self.titleArray[1];
            break;
        case 2:
            sectionName = self.titleArray[2];
            break;
        default:
            break;
    }
    return sectionName;
}
//section高度
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section {
    switch(section) {
        case 0:
            return 50;
            break;
        case 1:
            return 50;
            break;
        case 2:
            return 50;
            break;
        default:
            return 0;
            break;
    }
}
//row高度
-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section {
    return 40;
}

自定義section

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 40)];//

  //add your code behind

  

 return view;

}

header通過下面兩個代理方法設置

  • (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

  • (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

footer通過下面兩個

  • (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

  • (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

如果要做整個tableview的header和footer,要通過tableview setHeaderView setFooterView

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容