开发者

Custom TableViewCells on a UIViewController?

开发者 https://www.devze.com 2023-03-26 14:02 出处:网络
I am trying to display a custom TableView on a UIViewController but am getting an error \"UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:\"

I am trying to display a custom TableView on a UIViewController but am getting an error "UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:"

I had connected the TableView to datasource and delegate.

Any suggestion to go about implementing so or do I need a UITableViewController?

- (UITableViewCell *)tableView:(UITableView 开发者_Python百科*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomCell";

CustomCell *cell = (CustomCell *)
                    [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    NSArray *topLevelObjects = [[NSBundle mainBundle]
                                loadNibNamed:@"CustomCell"
                                owner:nil options:nil];
    for (id currentObjects in topLevelObjects){
        if ([currentObjects isKindOfClass:[UITableView class]]){
            cell = (CustomCell *) currentObjects;
            break;
        }
    }                           
}
//---set the text to display for the cell---
cell.cellNameLabel.text = @"This is name";
cell.cellValueLabel.text = @"This is Value";
return cell;


ERROR:

//NSArray *topLevelObjects = [[NSBundle mainBundle]
                                    loadNibNamed:@"CustomCell"
                                    owner:nil options:nil];

//in above owner should be self

//  if ([currentObjects isKindOfClass:[UITableView class]]){ 

change this line to

   if ([currentObjects isKindOfClass:[CustomCell class]]){



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"CustomCell";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle]
                                    loadNibNamed:@"CustomCell"
                                    owner:self options:nil];//owner should be self

        for (id currentObjects in topLevelObjects){
            if ([currentObjects isKindOfClass:[CustomCell class]]){
                cell = (CustomCell *) currentObjects;
                break;
            }
        }                           
    }
        //---set the text to display for the cell---
    cell.cellNameLabel.text = @"This is name";
    cell.cellValueLabel.text = @"This is Value";

    return cell;

}
0

精彩评论

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

关注公众号