如何显示静态自定义单元格

时间:2011-05-23 09:00:09

标签: objective-c cocoa-touch ios uitableview

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

Custom Table Cell enter image description here

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

接下来,我按如下方式配置我的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的正确方法吗?

1 个答案:

答案 0 :(得分:1)

您已在视图控制器中创建了自定义单元格,这是不可能的。使用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;
        }

    }
相关问题