UITableView代码块

@interface <#类名#> ()

@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray<__kindof NSDictionary *> *tableViewArr;
@end

static NSString *<#类名#>CellID = @"<#类名#>Cell";
@implementation <#类名#>

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationController.view.backgroundColor = [UIColor whiteColor];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.navigationItem.title = @"<#此类的title#>";
    [self setupTableViewArray];
    [self settingTableViewWay];
    
}
/**
 设置tableView的数据源
 */
- (void)setupTableViewArray {
    self.tableViewArr = [NSMutableArray arrayWithCapacity:0];
    [_tableViewArr addObject:@{@"class":@"<#要push过去的类名#>", @"title":@"<#push过去的类的title#>", @"details":@"<#push类的细节描述#>"}];
}

/**
 设置tableView
 */
- (void)settingTableViewWay {
    self.tableView = [[UITableView alloc] init];
    [self.view addSubview:_tableView];
    self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1 constant:0]];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 50;
    self.tableView.separatorColor = [UIColor orangeColor];//分隔线的颜色
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);//分隔线间距(上, 左, 下, 右)
    <#//不使用注册方式的cell#>[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:<#类名#>CellID];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
}
#pragma mark - UITableViewDataSource

/**
 返回section的row
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _tableViewArr.count;
}

/**
 返回indexPath的cell
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    <#//不使用注册方式的cell#>UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#类名#>CellID forIndexPath:indexPath];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#类名#>CellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:<#类名#>CellID];
    }
    cell.textLabel.text = [[_tableViewArr objectAtIndex:indexPath.row] objectForKey:@"title"];
    cell.detailTextLabel.text = [[_tableViewArr objectAtIndex:indexPath.row] objectForKey:@"details"];
    return cell;
}
#pragma mark - UITableViewDelegate

/**
 cell的点击事件
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *wayString = @"push";
    if ([wayString isEqualToString:@"push"]) {
        NSString *classString = [[_tableViewArr objectAtIndex:indexPath.row] objectForKey:@"class"];
        NSString *titleString = [[_tableViewArr objectAtIndex:indexPath.row] objectForKey:@"title"];
        Class tempClass = NSClassFromString(classString);
        UIViewController *vc = [[tempClass alloc] init];
        vc.view.backgroundColor = [UIColor whiteColor];
        vc.navigationItem.title = titleString;
        <#//不隐藏标签栏#>vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    } else if ([wayString isEqualToString:@"方法"]) {
        NSString *selString = [NSString stringWithFormat:@"selectRowAtIndexPath%zd", indexPath.row];
        SEL selector = NSSelectorFromString(selString);
        IMP imp = [self methodForSelector:selector];
        void (*func)(id, SEL) = (void (*)(id,SEL))imp;
        func(self,selector);
    } else {
        
    }
}
- (void)selectRowAtIndexPath<#row#> {

}
@end

你可能感兴趣的:(UITableView代码块)