iOS

点击cell跳转多个控制器

2016-03-29  本文已影响320人  何年何月

cell跳转多个控制器

Class class = NSClassFromString(className);

第一步:把cell的名称和要跳转到的控制器名声明成数组

@property (nonatomic, strong) NSMutableArray *titles;
@property (nonatomic, strong) NSMutableArray *classNames;

第二步:在viewDidLoad中初始化数组并复制

- (void)viewDidLoad {
    [super viewDidLoad];

    self.titles  = [NSMutableArray array];
    self.classNames = [NSMutableArray array];
    [self addCell:@"Text Attributes 1" class:@"YYTextAttributeExample"];
    [self addCell:@"Text Attributes 2" class:@"YYTextTagExample"];
    [self addCell:@"Text Attachments" class:@"YYTextAttachmentExample"];
    
    [self.tableView reloadData];

}
// 赋值方法
- (void)addCell:(NSString *)title class:(NSString *)className {
    [self.titles addObject:title];
    [self.classNames addObject:className];
}

第三步:cellForRowAtIndexPath中一切照常:

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

    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YY"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YY"];
    }
    cell.textLabel.text = _titles[indexPath.row];
    return cell;
}


第四步:跳转事件:(重点来了)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *className = self.classNames[indexPath.row];
    Class class = NSClassFromString(className);
    if (class) {
        UIViewController *ctrl = class.new;
        ctrl.title = _titles[indexPath.row];
        [self.navigationController pushViewController:ctrl animated:YES];
    }
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}


Demo下载:

上一篇下一篇

猜你喜欢

热点阅读