iOS tableView 基本设置

2021-09-03  本文已影响0人  安宇辛

一:添加tableView
添加<UITableViewDelegate,UITableViewDataSource>

self.tableView2 = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, 10, self.view.frame.size.width, self.scrollView.height - 150)];
    self.tableView2.backgroundColor = self.tableView.backgroundColor;
    self.tableView2.delegate = self;
    self.tableView2.dataSource = self;
    self.tableView2.separatorStyle = UITableViewCellSeparatorStyleNone;//去掉分割线
    self.tableView2.showsVerticalScrollIndicator = NO;//去掉滚动条
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ClickScreen)];
    tap.cancelsTouchesInView = NO;//是否取消点击处的其他action
    [self.tableView2 addGestureRecognizer:tap];
    self.tableView2.backgroundColor = [UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1];
    [self.scrollView addSubview: self.tableView2];

二:代理事件

#pragma mark - TableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //分组
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   //返回cell 个数  如果有两个tableView  可以通过判断是哪一个tableView 然后处理
    if(self.tableView == tableView){
        return self.list.count;
    }else{
        return self.list2.count;
    }
   
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   //处理每一个cell
    if(self.tableView == tableView){
        RepostViewCell *Cell = [RepostViewCell  cellWithTableView:tableView];
        return Cell;

     }else{
        RepostViewCell2 *Cell = [RepostViewCell2  cellWithTableView:tableView];
        return Cell;
     } 
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //设置cell 高度
    return  self.cellType==0?84:514;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     //cell 点击事件

}
上一篇 下一篇

猜你喜欢

热点阅读