如何将静态TableView添加到ViewController

时间:2013-10-20 10:03:53

标签: ios uitableview uiviewcontroller

如何在不使用容器和UITableViewController的情况下将静态TableView添加到ViewController中?

我认为这可能是前段时间了,但现在最新的xCode故事板和iOS 7却没有。 没有有效答案的类似问题在这里 - iOS 7 change UIView to UITableView 我的表有一点区别 - 我尝试添加静态数据。 我在程序启动时遇到应用程序崩溃。 如果我只设置数据源,程序崩溃。如果我取消链接数据源并保留委托和出口程序,程序启动时没有警告但是空单元格。 注释 - 在没有我帮助的情况下,Storyboard将ViewController的命名从ViewController命名为ViewViewController。我认为这是在添加IBOutlet UITableView * myTableView之后发生的。为什么这样,它是什么意思?

1 个答案:

答案 0 :(得分:0)

在UITableViewDataSource中有2个必需的方法:

– tableView:cellForRowAtIndexPath:  required method
– tableView:numberOfRowsInSection:  required method

这意味着在MYTableViewController中你必须实现这样的(例如3个单元格Test1,Test2和Test3):

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MyStaticCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"Test%d",indexPath.row+1];
    return cell;
}