繁体   English   中英

如何为使用xib用户界面创建的TableViewController设置静态表视图

[英]how to set a static table view for a TableViewController that is created with xib user interface

我创建了一个控制器类,该类扩展了UITableViewController和“带有XIB用户界面”选项。 XCode创建了3个文件,.h文件,.m文件和.xib文件。 当我打开xib文件时,它具有表格视图,但是无法像在故事板上的表格视图中那样将表格视图的内容设置为“静态单元格”。 您能否在这里指导我如何创建通过表视图控制器类创建的静态表视图?

嘿,您是说您创建了一个扩展UITableViewController的控制器类。 在类中扩展UITableViewController时,不需要从对象库中添加表视图(显示在左下角)。 Xcode设置了添加tableview的所有基本要求,还设置了您的委托和数据源方法,并实现了必需的委托方法

在这种情况下,您只需通过编程将数据(名称文本,详细信息文本)添加到表视图中

您将需要实现UITableView delegatedatasource方法。 -numberOfSections-numberOfRowsInSection:方法中返回静态值,并根据-cellForRowAtIndexPath:方法中的-cellForRowAtIndexPath:返回单元格。

为类创建NIB文件时,不能在Interface Builder中执行此操作。 如果需要,可以将其包含在情节提要中,然后在其中将单元格设置为静态。 但是,如果您希望在单独的NIB文件中创建它,则必须使用已生成的类。 这些类已经具有您将使用的所有功能。 如果您希望在表格中设置5行,每行中都有一个城市名称,则可以使用以下功能:

@property(nonatomic) NSArray* cities;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        cities = [NSArray arrayWithObjects:@"Alacante",@"Bournmouth",@"London",@"Manchester",@"Tyme", nil];
    }
    return self;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //Return the number of rows in the section.
    return 5;
}

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

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    cell.textLabel setText:[cities objectAtIndex:indexPath.row];

    return cell;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM