2011-05-23 25 views
0

我想创建一些静态表格视图单元格来存储我的内容。我的设置出现在画面我的笔尖文件:如何显示静态自定义单元格

Custom Table Cellenter 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的数据源必须的tableView返回细胞:的cellForRowAtIndexPath:”

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

回答

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; 
     } 

    } 
+0

嗨克里希南,谢谢你的回应。如果我需要每个角色都是不同的设计呢?我可以使用这种方法吗?其实我试图按照文档http://developer.apple.com/library/ios/#DOCUMENTATION/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html。在“静态行内容技术”部分下。但我必须在这里错过一些东西。任何建议表示赞赏!谢谢 – Zhen 2011-05-23 09:19:37

+0

这是我第一次看到这种方法。我会看到它并回应。 – Krishnan 2011-05-23 09:32:18

+0

我相信你必须错过控制器和XIb文件中的单元之间的IB连接。我开发了类似的应用程序并进行了测试我没有问题。你也可以尝试调试,看看infoCell的值是否为零或有一些值。 – Krishnan 2011-05-23 09:40:48

相关问题