码迷,mamicode.com
首页 > 其他好文 > 详细

UITableView (1)

时间:2014-10-22 12:41:49      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   使用   for   strong   

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;

 

UITableView (1)

标签:style   blog   color   io   os   ar   使用   for   strong   

原文地址:http://www.cnblogs.com/safiri/p/4042739.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!