iOS实现表格(非TableView)

作者: Whde | 来源:发表于2016-05-07 09:23 被阅读4077次
iOS表格

WhdeForm

iOS 表格<code>项目地址:https://github.com/whde/WhdeForm</code>

pod 'WhdeForm', '~> 1.0.0'
  • 添加了<code>Reusable</code>机制
  • 添加了横(<code>Section</code>)表头
  • 添加了竖(<code>Column</code>)表头
  • 添加了左上角(<code>TopLeftHeader</code>)总表头
  • 通过<code>FDateSource</code>去创建各个元素,类似<code>TableView</code>
  • 添加了<code>FIndexPath,{section, column}</code>
  • 表头添加点击事件
  • 添加了网格

使用

#import "ViewController.h"
#import "FormScrollView.h"
@interface ViewController ()<FDelegate, FDataSource> {
    NSArray *_data;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.autoresizingMask = UIViewAutoresizingNone;
    FormScrollView *table = [[FormScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64)];
    table.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    table.fDelegate = self;
    table.fDataSource = self;
    [self.view addSubview:table];
    
    _data = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"datas" ofType:@"plist"]];
    [table reloadData];
}
- (FTopLeftHeaderView *)topLeftHeadViewForForm:(FormScrollView *)formScrollView {
    FTopLeftHeaderView *view = [formScrollView dequeueReusableTopLeftView];
    if (view == NULL) {
        view = [[FTopLeftHeaderView alloc] initWithSectionTitle:@"行数" columnTitle:@"列数"];
    }
    return view;
}

- (NSInteger)numberOfSection:(FormScrollView *)formScrollView {
    return _data.count;
}
- (NSInteger)numberOfColumn:(FormScrollView *)formScrollView {
    return 100;
}
- (CGFloat)heightForSection:(FormScrollView *)formScrollView {
    return 44;
}
- (CGFloat)widthForColumn:(FormScrollView *)formScrollView {
    return 80;
}
- (FormSectionHeaderView *)form:(FormScrollView *)formScrollView sectionHeaderAtSection:(NSInteger)section {
    FormSectionHeaderView *header = [formScrollView dequeueReusableSectionWithIdentifier:@"Section"];
    if (header == NULL) {
        header = [[FormSectionHeaderView alloc] initWithIdentifier:@"Section"];
    }
    [header setTitle:[NSString stringWithFormat:@"第%ld行", (long)section] forState:UIControlStateNormal];
    /*[header setBackgroundColor:[UIColor redColor]];*/
    [header setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    return header;
}
- (FormColumnHeaderView *)form:(FormScrollView *)formScrollView columnHeaderAtColumn:(NSInteger)column {
    FormColumnHeaderView *header = [formScrollView dequeueReusableColumnWithIdentifier:@"Column"];
    if (header == NULL) {
        header = [[FormColumnHeaderView alloc] initWithIdentifier:@"Column"];
    }
    [header setTitle:[NSString stringWithFormat:@"第%ld列", (long)column] forState:UIControlStateNormal];
    /*[header setBackgroundColor:[UIColor greenColor]];*/
    [header setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    return header;
}
- (FormCell *)form:(FormScrollView *)formScrollView cellForColumnAtIndexPath:(FIndexPath *)indexPath {
    FormCell *cell = [formScrollView dequeueReusableCellWithIdentifier:@"Cell"];
    NSLog(@"%@", cell);
    if (cell == NULL) {
        cell = [[FormCell alloc] initWithIdentifier:@"Cell"];
        static int i=0;
        i++;
        NSLog(@"%d--%ld", i, (long)indexPath.section);
    }
    NSDictionary *dic = [_data objectAtIndex:indexPath.section];
    [cell setTitle:dic[@"name"] forState:UIControlStateNormal];
    [cell setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    /*[cell setBackgroundColor:[UIColor yellowColor]];*/
    return cell;
}
- (void)form:(FormScrollView *)formScrollView didSelectSectionAtIndex:(NSInteger)section {
    NSLog(@"Click Section At Index:%ld", (long)section);
}
- (void)form:(FormScrollView *)formScrollView didSelectColumnAtIndex:(NSInteger)column {
    NSLog(@"Click Cloumn At Index:%ld", (long)column);
}
- (void)form:(FormScrollView *)formScrollView didSelectCellAtIndexPath:(FIndexPath *)indexPath {
    NSLog(@"Click Cell At IndexPath:%ld,%ld", (long)indexPath.section, (long)indexPath.column);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end

相关文章

网友评论

  • 阿文灬:这个可以编辑或点击某一个单元格吗?
    Whde:@小蚊子叮迎行 没有做,你可以参照这个去实现,我这个,实际用起来不是很实用,你得根据自己的需求去改
  • 丶狂舞:请问怎么单独更改行数所在的那一列的宽度,其他的列均分
    丶狂舞:@海浪Whde 好吧,多谢了
    Whde:这个暂时没有考虑这个,你可以借鉴我这个去再深入去写,重要的一个是重用
  • dd25f9257b81:谢谢分享
  • dff31b46c8d8:您好,我想请问一下,表单里面的内容(人名)可以随着字体增多表格变宽么
    Whde:@强子0001 没有做这样的,我这只是一个思想,你可以在这基础上去做修改,也可以自己去写
  • 2134e6d4f39b:厉害:+1:🏻:+1:🏻
    Whde:@2134e6d4f39b 谢谢

本文标题:iOS实现表格(非TableView)

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