【问题标题】:how to set a static table view for a TableViewController that is created with xib user interface如何为使用 xib 用户界面创建的 TableViewController 设置静态表视图
【发布时间】:2014-03-11 11:10:12
【问题描述】:

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

【问题讨论】:

    标签: ios objective-c xcode ios7


    【解决方案1】:

    在为类创建 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;
    }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-11-07
          • 1970-01-01
          • 1970-01-01
          • 2010-09-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多