美文网首页
【极客班】TableView实例

【极客班】TableView实例

作者: xiongsirui | 来源:发表于2016-04-04 02:21 被阅读155次

动态TableView

在ViewController中添加一个Table View上去,选择Dynamic Prototypes,如下:

动态.png

除了必须实现dataSource里面的三个方法以外,还可以自定义首尾标识符:

//header
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView * header = [[UIView alloc] init];
    if ( section == 0 ) {
        header.backgroundColor = [UIColor lightGrayColor];
    } else {
        header.backgroundColor = [UIColor darkGrayColor];
    }
    return header;
}
//footer
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView * footer = [[UIView alloc] init];
    if ( section == 0 ) {
        footer.backgroundColor = [UIColor blueColor];
    } else {
        footer.backgroundColor = [UIColor cyanColor];
    }
    return footer;
}

静态TableView

在ViewController中添加一个Table View上去,选择Static Prototypes,如下:


image.png

静态方法不用实现dataSource中的三个方法,因为在tableView的UI上已经做好了,该方法适合不用更新的tableView:


image.png image.png

但是可以实现delegate方法,点击该Cell时,会跳转到其他界面:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  if ( indexPath.section == 0 && indexPath.row == 1 ) {
    [self performSegueWithIdentifier:@"showWiFi" sender:self];
  } else {
    // TODO : handle it!
  }
}

Wife的界面中向下拉tableView会出现如菊花状的图形,代码如下:

- (IBAction)startRefresh:(id)sender {
  self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"scanning"];
}

- (void) stopRefreshing {
  [self.refreshControl endRefreshing];
  [self.tableView reloadData];
}

把Xib当Cell加载进TableView

设计一个Xib如下:


image.png

在XibCell.m文件中定义初始化函数:

- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    // The Xib will not be loaded automatically in super's -initWithStyle:reuseIdentifier:
    NSArray * uiObjects = [[NSBundle mainBundle] loadNibNamed:@"XibCell" owner:self options:nil];
    self = uiObjects[0]; // don't do this in real project, it is only for demonstration, register the xib instead.
    return self;
}

在ViewController中加载该Xib:

    UINib * nib = [UINib nibWithNibName:@"XibCell" bundle:nil]; // nil means [UIBundle mainBundle]
    [self.tableView registerNib:nib forCellReuseIdentifier:CELL_ID];
    self.tableView.rowHeight = 120.0f;
    [self.tableView registerClass:[XibCell class] forCellReuseIdentifier:CELL_ID];

若需要在ViewConrtoller中调用Xib中的按钮响应,需要添加代理:

@protocol btnClickedDelegate <NSObject>
-(void)cellBtnClicked;
@end

@interface MyXibTableViewCell : UITableViewCell
@property (nonatomic,weak) id<btnClickedDelegate>  btnDelegate;

排序与索引

要对tableview中的cell排序方法如下:

[Array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
       return [obj1 compare:obj2 options:NSCaseInsensitiveSearch];
                  }];

得到该数组的首字母,并分好有几个section,对其首字母分类:

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return [_secIndex count];
}

并设置header的title:

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return [_secIndex count];
}

这样就可以做好分类了,接下来一个函数就可以实现索引:

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return [_secIndex count];
}

相关文章

  • 【极客班】TableView实例

    动态TableView 在ViewController中添加一个Table View上去,选择Dynamic Pr...

  • 【极客班】tableview基础

    UITableView的结构 UITableVIew包含多个UITableVIewCell,而UITableVIe...

  • 【极客班】属性和实例变量

    重点掌握:1.结构只有实例变量2.类有下列成员:实例变量是对内的,反映内部状态,属性是对外的。属性表达的是实例状态...

  • 属性和实例变量(极客班)

    定义属性后,编译器自动生成set属性方法,get方法,及一个实例变量; 但是,用了同步后,下滑线同步变量就不能直接...

  • 关于类和实例(极客班)

    首先,堆和栈合在一起说,是因为操作系统会把堆和栈同放在一个存储空间共用,通过指针的偏移判断是否溢出; 其次,类,在...

  • 【极客班】ScrollView

    ScrollView是为控件或者布局添加滚动条. UIScrollViewDelegate 常用属性 Scroll...

  • 【极客班】AFNetworking

    AFNetWoring下载地址:https://github.com/AFNetworking/AFNetwork...

  • 【极客班】UITableView

    UITableView作为iOS非常重要的一部分,应当着重讨论 UITableView 学习地图 基本用法-->c...

  • React 参考汇总

    ECMAScript 6 入门 React 入门实例教程 中文文档地址 极客学院整理的文档 React 菜鸟教程 ...

  • 【极客班】Autolayout基础

    Autoresizing 如上图所示,如果只是定义控件和上级视图之间的约束,可以考虑用Autoresizing;V...

网友评论

      本文标题:【极客班】TableView实例

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