UITableView的使用和點擊每個cell跳轉到其他controller優化

一、需要一個NSObject類,該類代碼如下

#import <Foundation/Foundation.h>

@interface MJSampleIndex : NSObject

@property (nonatomic, copy) NSString *title;  //描述該類的title

@property (nonatomic, assign) Class controllerClass; //跳轉到的controller

+ (instancetype)sampleIndexWithTitle:(NSString *)title controllerClass:(Class)controllerClass;  //初始化title和controller

@end


#import "MJSampleIndex.h"


@implementation MJSampleIndex

+ (instancetype)sampleIndexWithTitle:(NSString *)title controllerClass:(Class)controllerClass

{

    MJSampleIndex *si = [[self alloc] init];

    si.title = title;

    si.controllerClass = controllerClass;

    return si;

}

@end

二、UITableViewController方法  

實現三個方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section //tableview總行數

{

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath //根據行數繪製tableviewcell

{

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  //點擊每個cell執行的事件

{

}

#import "MJSampleIndexViewController.h"

#import "MJSampleIndex.h"

#import "MJTableViewController.h"

#import "MJCollectionViewController.h"


NSString *const MJSampleIndexCellIdentifier = @"Cell";


@interface MJSampleIndexViewController ()

{

    NSArray *_sampleIndexs;

}

@end


@implementation MJSampleIndexViewController


- (void)viewDidLoad

{

    [super viewDidLoad];

    

    self.title = @"快速集成下拉刷新";

    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:nil action:nil];

    // 1.註冊cell

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:MJSampleIndexCellIdentifier];

    // 2.初始化數據

    MJSampleIndex *si1 = [MJSampleIndex sampleIndexWithTitle:@"tableView刷新演示" controllerClass:[MJTableViewController class]];

    MJSampleIndex *si2 = [MJSampleIndex sampleIndexWithTitle:@"collectionView刷新演示" controllerClass:[MJCollectionViewController class]];

    _sampleIndexs = @[si1, si2];

}


#pragma mark - Table view data source

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

{

    return _sampleIndexs.count;

}


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

{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MJSampleIndexCellIdentifier forIndexPath:indexPath];

    // 1.取出模型

    MJSampleIndex *si = _sampleIndexs[indexPath.row];

    // 2.賦值

    cell.textLabel.text = si.title;

    return cell;

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    // 1.取出模型

    MJSampleIndex *si = _sampleIndexs[indexPath.row];

    

    // 2.創建控制器

    UIViewController *vc = [[si.controllerClass alloc] init];

    vc.title = si.title;


    // 3.跳轉

    [self.navigationController pushViewController:vc animated:YES];

}


@end



發佈了29 篇原創文章 · 獲贊 7 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章