美文网首页
UITableView的简单介绍

UITableView的简单介绍

作者: Mustard_Buli | 来源:发表于2016-03-16 15:54 被阅读65次

    UITableView是继承与UIScrollView的。

    直接上代码:

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)style:UITableViewStyleGrouped];分组形式的
        self.myTableView.delegate = self;
        self.myTableView.dataSource = self;     <UITableViewDelegate,UITableViewDataSource>两个代理都要遵守
        self.myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;     分割线
        [self.myTableView setEditing:YES];     编辑状态
        [self.view addSubview:_myTableView];
    }
    
    //有多少段 默认为0
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 26;
    }
    
    //有多少行,调用多次
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        if ((section+1)%2 == 0) {
            return 3;
        }else{
            return 2;
        }
    }
    
    //某段某行显示什么样子  NSIndexPath封装的两个属性section和row
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        //重复利用机制
        NSString *cellID = @"cellID";
        //先判断是否有可以重复利用的cell
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (cell==nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        //cell显示的样式
        cell.textLabel.text = [NSString stringWithFormat:@"%ld,%ld",indexPath.section,indexPath.row];
        return cell;
    }
    
    //配置cell高度
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return 80;
    }
    
    //段落头部尾部
    -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
        return @"头标题";
    }
    -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
        return @"尾标题";
    }
    -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        return 30;
    }
    -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
        return 30;//不能没有,如果想没有就把值变小
    }
    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
        UIView * v =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
        v.backgroundColor = [UIColor redColor];
        return v;
    }
    
    //cell被点了要做什么
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        [tableView deselectRowAtIndexPath:indexPath animated:YES];取消点击事件
    }
    
    //设置cell是否可以编辑
    -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
        if (indexPath.row== 0) {
            return NO;
        }else{
            return YES;
        }
    }
    
    //更改编辑状态的按钮
    -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath{
        return  UITableViewCellEditingStyleDelete;
    }
    
    //更改删除状态下右边显示的标题
    -(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexPath{
        return @"删除";
    }
    
    //删除状态下按钮被点
    -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    }
    
    //cell可以上下移动
    -(bool)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
        return YES;
    }
    -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{  
    }
    
    //索引标题
    -(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
        NSMutableArray * titleArray = [NSMutableArray array];
        for (int i = 0; i<26; i++) {
            [titleArray addObject:[NSString stringWithFormat:@"%c",'A'+i]];
        }
        return titleArray;
    }
    

    相关文章

      网友评论

          本文标题:UITableView的简单介绍

          本文链接:https://www.haomeiwen.com/subject/idwqlttx.html