【问题标题】:UITableViewController Delegate and DataSource method not called未调用 UITableViewController 委托和数据源方法
【发布时间】:2016-03-03 19:52:06
【问题描述】:

我是 ios 开发的新手,我正在尝试使用 HCPerson 类的 Array 填充表视图。我也在玩通过代码将在 nib 中创建的对象带到情节提要中。但无论我做什么,表格都不会被填充

#import "HCTableViewController.h"
NSString *const HCTableCellNibName=@"HCTableCell";
NSString *const HCCellIdentifier=@"personCell";
@interface HCTableViewController()

@end
@implementation HCTableViewController
-(void)viewDidLoad
{
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:HCTableCellNibName bundle:nil] forCellReuseIdentifier:HCCellIdentifier];

}


-(NSInteger)tableView:(UITableView*)tableView numberOfRowInSection:(NSInteger)section
{
    return self.persons.count;
}

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    UITableViewCell* cell=(UITableViewCell*)[self.tableView dequeueReusableCellWithIdentifier:HCCellIdentifier forIndexPath:indexPath];
    HCPerson *person=self.persons[indexPath.row];

    [self configureCell:cell forPerson:person];
    return cell;

}

-(void)configureCell:(UITableViewCell*)cell forPerson:(HCPerson*)person
{
    cell.textLabel.text=person.name;
    cell.detailTextLabel.text=[[NSNumberFormatter alloc] stringFromNumber:person.age];
}
@end

//HCPerson
#import "HCPerson.h"

@interface HCPerson()

@end
@implementation HCPerson
+(HCPerson*)personWithName:(NSString*)name age:(NSNumber*)age residence:(NSString*)residence contact:(NSString *)contact
{
    HCPerson *newPerson=[[self alloc] init];

    newPerson.name=name;
    newPerson.age=age;
    newPerson.residence=residence;
    newPerson.contact=contact;

    return newPerson;
}
@end

编辑:Karthik 使用 UIViewController 解决了这个问题,并从中输出 TableView。但是有什么方法可以使用 UITableViewController 调用委托和数据源方法

【问题讨论】:

  • 您在模型类中添加对象的位置
  • 应用委托应用程序完成启动
  • 您是否尝试调试过委托和数据源方法是否被调用?
  • 调用了委托方法,但是当我将 NSLog 放入数据源方法时,我什么也看不到
  • 这意味着你的数据源方法没有被调用。

标签: ios objective-c xcode uitableview cocoa-touch


【解决方案1】:

好的,让我们试试你的 viewDidLoad

-(void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:HCTableCellNibName bundle:nil] forCellReuseIdentifier:HCCellIdentifier];

 if ( self.persons.count>0)
{
[self.yourtableview reloadData];
}
else
{
// no data found , put the NSLog and print the count

}

好的选择第二个

试试这个

UITableViewCell* cell=(UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:HCCellIdentifier forIndexPath:indexPath];

删除那个self,因为它显示保留了那个方法。不是您的Tableview,也可以尝试使用与tableView 不同的名称,因为Xcode 发现很难从中选择一个。

【讨论】:

  • if (self.persons.count>0) is true with count=4 但tableview中仍然没有数据
  • 检查更新的答案兄弟,另一个把断点放在numberOfRowInSection 并检查数据源方法是否被调用
  • 否则在[self.yourtableview reloadData];ViewDidAppear中添加此方法
【解决方案2】:

您是否从情节提要中添加了表格视图控制器? 如果是,请转到 storyborad -> Inspector Area -> File Inspector - 在此处添加您的类名。

Screenshot attached.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    相关资源
    最近更新 更多