我正在尝试创建一些静态表视图单元格来存储我的内容。如图所示,我设置了笔尖文件:



本质上,我只有一个静态单元格,其中包含地图视图和标签。

接下来,我将Xcode文件配置如下:

标头:

@interface CarParkDetailViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>
{
    UITableViewCell *infoCell;
    MKMapView *detailMapView;
    UILabel *addressLabel;

}

@property (nonatomic, retain) IBOutlet UITableViewCell *infoCell;
@property (nonatomic, retain) IBOutlet MKMapView *detailMapView;
@property (nonatomic, retain) IBOutlet UILabel *addressLabel;

@end


实现方式:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}


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


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return infoCell;
}


上面的代码不起作用。 (乍一看似乎不对)。我收到以下错误


  ***由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“ UITableView dataSource必须从tableView:cellForRowAtIndexPath返回单元格:”


谁能告诉我显示我的infoCell正确的方法是什么?

最佳答案

您无法在视图控制器中创建自定义单元格。使用IB创建自定义单元并在那里设计您的UI。然后在cellForRowAtIndex方法中使用以下代码:

CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:identifier];

if (cell == nil) {
    NSArray *nibObjects=[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
    for (id currentObject in nibObjects) {
        if ([currentObject isKindOfClass:[CustomCell class]]) {
            cell = (CustomCell *) currentObject;
            break;
        }

    }

关于objective-c - 如何显示静态自定义单元格,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6095007/

10-10 20:41
查看更多