Ios 未调用UITableViewController委托和数据源方法
我是ios开发人员中的新手,我正在尝试使用HCPerson类的数组填充表视图。我还通过代码将nib中创建的对象带到情节提要中。但不管我做什么,桌子都不会被填满Ios 未调用UITableViewController委托和数据源方法,ios,objective-c,xcode,uitableview,cocoa-touch,Ios,Objective C,Xcode,Uitableview,Cocoa Touch,我是ios开发人员中的新手,我正在尝试使用HCPerson类的数组填充表视图。我还通过代码将nib中创建的对象带到情节提要中。但不管我做什么,桌子都不会被填满 #import "HCTableViewController.h" NSString *const HCTableCellNibName=@"HCTableCell"; NSString *const HCCellIdentifier=@"personCell"; @interface HCTableViewController() @
#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调用委托和数据源方法呢
-(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
}
ok第二选择
试试这个
UITableViewCell* cell=(UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:HCCellIdentifier forIndexPath:indexPath];
移除那个自我,因为它表明它是那个方法的一部分。不是您的
Tableview
,也尝试使用与Tableview
不同的名称,因为Xcode
发现很难从它们中选择一个。您是否从情节提要中添加了表视图控制器?
如果是,请转到storyborad->Inspector Area->File Inspector-在那里添加您的类名
您在模型classApp委托应用程序中添加对象的位置IDFinishLaunching是否尝试并调试委托和数据源方法是否正在被调用?委托方法已被调用,但当我将NSLog放入数据源方法中时,我看不到任何内容,这意味着您的数据源方法未被调用。如果(self.persons.count>0)在count=4时为真,但TableView中仍然没有数据检查更新的答案bro,另一个将断点放在
numberOfRowInstitution
中,并检查是否调用了数据源方法或注意事项。请在[self.yourtableview reloadData]中添加此方法;
在视图中显示