开发者

How to display Static Custom Cell

开发者 https://www.devze.com 2023-03-07 13:52 出处:网络
I am trying to create some static table view cells to store my contents. I set up my nib file as seen in the picture:

I am trying to create some static table view cells to store my contents. I set up my nib file as seen in the picture:

How to display Static Custom Cell

How to display Static Custom Cell

In essence, I only have one static cell, containing a map view and a label.

Next I configure my Xcode files as follows:

Header:

@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

Implementation:

开发者_运维百科- (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;
}

The code above doesn't work. (Didn't seem right in the first place). I am getting the following error

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

Can anyone advise me on what the correct approach is in displaying my infoCell?


You have created a custom cell in a view controller, which is not possible. Use IB to create a custom cell and design your UI there. Then use the following code in your cellForRowAtIndex method:

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

精彩评论

暂无评论...
验证码 换一张
取 消