zoukankan      html  css  js  c++  java
  • UITableView (1)

    1.实例化 Table View 并设置一个Delegate

    2.向TableView填充数据

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        self.view.backgroundColor = [UIColor whiteColor];
        _myTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        /*Make sure our tableview resizes correctly*/
        _myTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
        [self.view addSubview:_myTableView];
    }

    一个TableView的数据源能够实现三个重要方法,其中2个必须实现

    @required

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

    首先告诉TableView显示3个section:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        NSInteger result = 0;
        if ([tableView isEqual:_myTableView]) {
            result = 3;
        }
        return result;
    }

    然后告诉每个Section显示多少行:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        NSInteger result = 0;
        if ([tableView isEqual:_myTableView]) {
            switch (section) {
                case 0:
                    result = 3;
                    break;
                case 1:
                    result = 5;
                    break;
                case 2:
                    result = 8;
                    break;
                default:
                    break;
            }
        }
        return result;
    }

    最后返回TableViewCell的静态实例给表示图:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *result = nil;
        if ([tableView isEqual:_myTableView]) {
            static NSString *TableViewCellIdentifier = @"MyCells";
            result = [tableView dequeueReusableCellWithIdentifier:TableViewCellIdentifier];
            if (result == nil) {
                result = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableViewCellIdentifier];
            }
            result.textLabel.text = [NSString stringWithFormat:@"Section%ld,Cell%ld",indexPath.section,indexPath.row];
        }
        return result;
    }

    3.接收和处理TableView事件,例如

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        if ([tableView isEqual:_myTableView]) {
            NSLog(@"%@",[NSString stringWithFormat:@"Cell %ld in Section %ld is selected",indexPath.row,indexPath.section]);
        }
    }

    4.在TableView中使用不同种类的附件

    (通过展示不同附件(accessories),你想抓住用户对一个tableview的关注,或者提供不同的方式使用户在tableview中和每个cells进行互动)

    每个cell的accessoryType属性是UITableViewCellAccessoryType枚举类型的:

    typedef NS_ENUM(NSInteger, UITableViewCellAccessoryType) {

        UITableViewCellAccessoryNone,                   // don't show any accessory view

        UITableViewCellAccessoryDisclosureIndicator,    // regular chevron. doesn't track

        UITableViewCellAccessoryDetailDisclosureButton, // info button w/ chevron. tracks

        UITableViewCellAccessoryCheckmark,              // checkmark. doesn't track

        UITableViewCellAccessoryDetailButton NS_ENUM_AVAILABLE_IOS(7_0) // info button. tracks

    };

    注意:UITableViewCellAccessoryDetailDisclosureButton的按钮在被点击时会向委托触发一个事件.因此,detail disclosure按钮允许用户在同一行上执行两个独立相关的操作.此按钮点击时会产生一个被tableview的委托对象捕获的事件:

    - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
         NSLog(@"Accessory button is tapped for cell at index path = %@", indexPath);
        UITableViewCell *ownerCell = [tableView cellForRowAtIndexPath:indexPath];
        NSLog(@"Cell Title = %@", ownerCell.textLabel.text);
    }

    4.创建自定义TableView单元格附件

    方案:把类型UIView的一个实例分配到任何UITableViewCell类的实例的accessoryView 的属性:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell* result = nil;
        static NSString *MyCellIdentifier = @"SimpleCell";
        result = [tableView dequeueReusableCellWithIdentifier:MyCellIdentifier];
        if(result == nil){
            result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyCellIdentifier];
        }
        result.textLabel.text = [NSString stringWithFormat:@"Section %ld, Cell %ld", (long)indexPath.section,(long)indexPath.row];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = CGRectMake(0.0f, 0.0f, 150.0f, 25.0f);
        [button setTitle:@"Expand" forState:UIControlStateNormal];
        [button addTarget:self
                   action:@selector(performExpand:)
         forControlEvents:UIControlEventTouchUpInside];
        result.accessoryView = button;
        return result;
    }

    那么点击按钮事件发送的按钮属于哪一个单元格呢?所以,我很要以某种方式把我们的按钮与其所属的cell连接起来

    - (void)performExpand:(UIButton *)paramSender{
        UITableViewCell *ownerCell = (UITableViewCell *)[paramSender superview];
        if (ownerCell != nil) {
            NSIndexPath *ownerCellIndexPath = [_myTableView indexPathForCell:ownerCell];
            if (ownerCellIndexPath.section == 0&& ownerCellIndexPath.row == 1) {
                //do something
            }
        }
    }

    5.在tableView中展示分层数据

    方案:使用TableViewCell的缩进功能:

    //使用tableViewcell的缩进功能 有两个相关属相:缩进等级和缩进宽度,两者相乘就是偏移量,根据这个偏移量cell的内容会向左右两侧偏移,正数向右,负数向左
            UITableViewCell *result = nil;
    .....
         result.indentationLevel = indexPath.row;
            result.indentationWidth = 10.0f;
  • 相关阅读:
    对我最重要的三个老师
    究竟 javascript 错误处理有哪些类型?
    前端小白凭什么杀进 BAT?
    金九银十,换工作热潮来临!
    适用于Java开发人员的SOLID设计原则简介
    在5分钟内将Spring Boot作为Windows服务启动
    Redis中的Java分布式缓存
    Arrays工具类详解
    SpringBoot整合redis
    java 数组用法详解
  • 原文地址:https://www.cnblogs.com/safiri/p/4042739.html
Copyright © 2011-2022 走看看