UITableView

UITableView:表视图
表视图通常用来管理一组具有相同数据结构的数据
UITableView继承于UIScrollView,所以可以滚动
表视图的每一条数据都是显示在UITableViewCell对象中
表视图可以分区显示数据,每一个分区称为一个section, 每一行称为一个row, 编号都是从0开始.

表视图的创建:
重要属性:
1.style样式: plain, group.
2.分割线样式:separatorStyle
3.分割线颜色:separatorColor
4.行高:rowHeight

一个tableView里面可以有多个重用池
po调试的时候,若内容显示为空,则这个方法一定没有初始化
解决问题方法:断点 po 输出NSLog 

cell自定义 放两个label 重用池
数组套字典

1.使用系统自定义的各种UITableViewCell的样式

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString* indentifier = @"cell";
    MyTableCell* cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        
        cell = [[[MyTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier]autorelease];
    }
    cell.textLabel.text = [_data objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = @"detail";
    cell.imageView.image = [UIImage imageNamed:@"checkmark.png"];
    return cell;
}

使用UITableViewCellStyleDefault的效果:


使用UITableViewCellStyleValue1的效果:



使用UITableViewCellStyleValue2的效果:


在UITableViewCell内默认是有contentview和accessoryView这两个subview的,contentview中的subview根据不同的cell的style会使用不同的布局。contentview和其中的默认subview会根据cell的编辑状态出现的控件自动缩进,自定义cell时可以把自定义控件添加在contentview中,也可以直接添加到cell中。


2.设置UITableViewCell的属性

      //cell的右边辅助按钮的样式
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    //自定义cell右边的辅助按钮
    cell.accessoryView = nil;
    //自定义cell的背景
    cell.backgroundView = nil;
    //设置cell的contentview中的detail的文字内容
    cell.detailTextLabel.text = @"";
    //查看cell当前的编辑模式
    int style = cell.editingStyle;
    //设置当cell进入编辑模式时的辅助按钮样式
    cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
    //自定义cell进入编辑模式后辅助按钮
    cell.editingAccessoryView = nil;
    //获取cell的缩进级别
    int level = cell.indentationLevel;
    //获取cell的缩进宽度
    float width = cell.indentationWidth;
    //设置cell被选中时的背景
    cell.selectedBackgroundView = nil;
    //设置cell的选中状态样式
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    //设置cell的contentview中的textlabel文字内容
    cell.textLabel.text = @"";


3.自定义的UITableViewCell重写父类的方法

//初始化uitableviewcell后,自定义cell添加subview
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

//当cell被选中时,uitableview内部会自动调用该方法,重写该方法可以在cell被选中时做一些额外的操作
- (void)setSelected:(BOOL)selected animated:(BOOL)animated

//当cell处于高亮状态时,uitableview内部会自动调用该方法,重写该方法可以在cell处于高亮时做一些额外操作
-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated

//重写layoutsubviews方法,为了查看当cell改变编辑状态时,有什么subview
-(void)layoutSubviews{

    [super layoutSubviews];
    NSArray* subs = self.subviews;
    for (UIView* sub in subs) {
        NSLog(@"view:%@",sub);
    }
}

当进入删除编辑模式时,cell的subview有一个叫UITableViewCellDeleteConfirmationControl的subview,这代表删除按钮。可以修改该view达到修改删除按钮的位置,大小等属性。

当进入移动编辑模式时,cell的subview有一个叫UITableViewCellReorderControl的subview,这个代表移动按钮。可以修改该view达到修改移动按钮的位置,大小等属性。

当进入插入编辑模式时,cell的subview有一个叫UITableViewCellEditControl的subview,这个代表添加按钮。可以修改该view达到修改添加按钮的位置,大小等属性。


//当cell的状态变为编辑时,uitableview内部会自动调用该方法,重写该方法可以改变cell的布局
-(void)willTransitionToState:(UITableViewCellStateMask)state{
    [super willTransitionToState:state];
}
//当cell的状态变为编辑时,uitableview内部会自动调用该方法,重写该方法可以改变cell的布局
-(void)didTransitionToState:(UITableViewCellStateMask)state{
    [super didTransitionToState:state];
    
    //滑动出现的删除按钮state是2的,编辑状态下的删除按钮state是3的
    if (state == UITableViewCellStateShowingDeleteConfirmationMask||state==3) {
        for (UIView *subview in self.subviews) {
            //cell的subview为UITableViewCellDeleteConfirmationControl时,代表是删除按钮
            if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) {
                
                UIView *deleteButtonView = subview;
                CGRect f = deleteButtonView.frame;
                f.origin.x -= 50;
                deleteButtonView.frame = f;            }
        }
    }
    //插入和移动的编辑状态state都是1
    else if(state==UITableViewCellStateShowingEditControlMask){
        for (UIView *subview in self.subviews) {
            NSString* type = @"";
            //判断如果cell当前是插入模式,则寻找UITableViewCellEditControl的subview,代表添加按钮
            if (self.editingStyle==UITableViewCellEditingStyleInsert) {
                type = @"UITableViewCellEditControl";
            }
            //否则寻找UITableViewCellReorderControl的subview,代表移动按钮
            else type = @"UITableViewCellReorderControl";
            if ([NSStringFromClass([subview class]) isEqualToString:type]) {
                
                UIView *deleteButtonView = [subview.subviews objectAtIndex:0];
                CGRect f = deleteButtonView.frame;
                f.origin.x -= 50;
                deleteButtonView.frame = f; 
            }
        }

    }
}


4.UITableViewCell自定义背景颜色

方法1:

通过修改contentview的backgroundcolor


方法2:

创建一个uiview,设置它的backgroundcolor后再添加到cell里


方法3:

通过在- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath的回调中设置cell的backgroundcolor



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在UITableView的section中添加数据,你需要先创建一个包含所需数据的数组。然后,在UITableViewDataSource协议中实现以下方法: 1. numberOfSections(in tableView: UITableView) -> Int:返回表格中的section数。 2. tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int:返回指定section中的行数。 3. tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell:返回指定indexPath的UITableViewCell实例。 例如,假设你有一个包含多个section的UITableView,每个section都包含一个字符串数组。以下是一个示例代码: ``` class ViewController: UIViewController, UITableViewDataSource { var data: [[String]] = [["item 1", "item 2"], ["item 3", "item 4", "item 5"]] @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() tableView.dataSource = self } // MARK: - UITableViewDataSource func numberOfSections(in tableView: UITableView) -> Int { return data.count } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return data[section].count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) cell.textLabel?.text = data[indexPath.section][indexPath.row] return cell } } ``` 在这个例子中,我们创建了一个包含两个section的UITableView。每个section都有一个字符串数组,我们将其存储在data数组中。在numberOfSections方法中,我们返回data数组的数量,即section的数量。在tableView(_:numberOfRowsInSection:)方法中,我们返回特定section中的行数。最后,在tableView(_:cellForRowAt:)方法中,我们获取特定indexPath的字符串并将其显示在UITableViewCell中。 注意,在上述示例代码中,我们将UITableViewCell标识符设置为“Cell”,你需要确保在Storyboard或xib文件中对应的UITableViewCell的标识符也设置为“Cell”。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值