簡體   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