【问题标题】:UITableView in UIViewController - Connecting cells to UIViewControllerUIViewController 中的 UITableView - 将单元格连接到 UIViewController
【发布时间】:2012-10-12 14:11:26
【问题描述】:

我在 UIViewController 中有一个 UITableView。我在 Storyboard 中创建了其他 UIViewControllers 我想连接到我的单元格。

如何将 Storyboard 中创建的 UIViewControllers 连接到我的单元格?


MyViewController.h

@interface MyViewController : UIViewController <UITableViewDelegate,    UITableViewDataSource> {
    IBOutlet UITableView *myTableView;
}

@property (nonatomic, retain) UITableView *myTableView;

MyViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *array = [[NSArray alloc] initWithObjects:@"CELL1", @"CELL2 & CELL3", nil];
    self.listData = array;
    [array release];

    [myTableView setDataSource:self];
    [myTableView setDelegate:self];

    [[self view] addSubview:myTableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [listData count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UpdatesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"UpdatesTableViewCell" owner:nil options:nil];

        for (UIView *view in views) {
            if([view isKindOfClass:[UITableViewCell class]])
            {
                cell = (UpdatesTableViewCell*)view;
            }
        }
    }

    NSUInteger row = [indexPath row];
    cell.textLabel.text = [listData objectAtIndex:row];

    return cell;
}

【问题讨论】:

    标签: objective-c uitableview uiviewcontroller storyboard


    【解决方案1】:

    您可以创建从这个控制器到其他控制器的转场,然后选择当有人点击单元格时要执行的转场,或者您可以使用对故事板的引用来调用instantiateViewControllerWithIdentifier: 并使该控制器在点击时处于活动状态。

    【讨论】:

    • 谢谢,我想使用情节提要的引用来调用 instantiateViewControllerWithIdentifier 但我不知道在哪里调用它以及如何调用它? (在 didSelectRowAtIndexPath 中?)
    • 这似乎是正确的地方。如果您当前的视图控制器来自同一个故事板,那么self.storyboard 应该为您提供可用于进行调用的参考。
    猜你喜欢
    • 1970-01-01
    • 2012-07-14
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多