iOS-ViewController中的第三个TableView未加载nib
我试图在我的iOS-ViewController中的第三个TableView未加载nib,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我试图在我的ViewController中填充3个不同的tableview(不同的tableview根据所选的UISegmentControl显示)-sidetableView、neigursview。前两个完全填充,但是第三个(neightursview)似乎没有加载单元格。请参阅下面的代码。你知道为什么吗?我觉得我在viewdidLoad中定义的第三个nib不对 ViewController.h - (IBAction)segmentControl:(id)sender; @property
ViewController
中填充3个不同的tableview
(不同的tableview根据所选的UISegmentControl
显示)-sidetableView
、neigursview
。前两个完全填充,但是第三个(neightursview
)似乎没有加载单元格。请参阅下面的代码。你知道为什么吗?我觉得我在viewdidLoad
中定义的第三个nib不对
ViewController.h
- (IBAction)segmentControl:(id)sender;
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentControl;
@property (weak, nonatomic) IBOutlet UITableView *sidetableView;
@property (strong, nonatomic) NSMutableArray *myFriendData;
@property (weak, nonatomic) IBOutlet UITableView *neighboursView;
@property (strong, nonatomic) NSMutableArray *friendData;
@property (strong, retain) NSMutableArray *neighbourData;
@property (strong, nonatomic) IBOutlet UITableView *friendsView;
- (IBAction)segmentControl:(id)sender;
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentControl;
@property (weak, nonatomic) IBOutlet UITableView *sidetableView;
@property (strong, nonatomic) NSMutableArray *myFriendData;
@property (weak, nonatomic) IBOutlet UITableView *neighboursView;
@property (strong, nonatomic) NSMutableArray *friendData;
@property (strong, retain) NSMutableArray *neighbourData;
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
UINib *nib = [UINib nibWithNibName:@"sidebarCell" bundle:nil];
[self.friendsView registerNib:nib forCellReuseIdentifier: @"sidebarCell"];
nib = [UINib nibWithNibName:@"MyFriendTableViewCell" bundle:nil];
[self.sidetableView registerNib:nib forCellReuseIdentifier: @"MyFriendTableViewCell"];
nib = [UINib nibWithNibName:@"sidebarCell" bundle:nil];
[self.neighboursView registerNib:nib forCellReuseIdentifier: @"sidebarCell"];
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.sidetableView) {
if ([self.myFriendData count] > 0) {
return [self.myFriendData count];
} else {
return 0;
}
}
if (tableView == self.friendsView) {
if ([self.friendData count] > 0) {
return [self.friendData count];
} else {
return 0;
}
}
else {
// return [self.neighbourData count];
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView == self.sidetableView) {
self.sidetableView.separatorStyle = UITableViewCellSeparatorStyleNone;
MyFriendTableViewCell *cell = (MyFriendTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"MyFriendTableViewCell"];
NSDictionary *friendsName = [self.myFriendData objectAtIndex:indexPath.row];
[[cell friendName] setText:[friendsName objectForKey:@"title"]];
NSDictionary *friendsBio = [self.myFriendData objectAtIndex:indexPath.row];
[[cell friendBio] setText:[friendsBio objectForKey:@"field_friendbio"][@"und"][0][@"safe_value"]];
NSString *profilePath = [[self.myFriendData objectAtIndex:indexPath.row] objectForKey:@"field_picture"][@"und"][0][@"safe_value"];
[cell.friendPic sd_setImageWithURL:[NSURL URLWithString:profilePath]];
NSLog(@"This is profilePath %@",profilePath);
return cell;
}
if (tableView == self.friendsView)
{
self.friendsView.separatorStyle = UITableViewCellSeparatorStyleNone;
sidebarCell *cell = (sidebarCell *)[tableView dequeueReusableCellWithIdentifier:@"sidebarCell"];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"sidebarCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
NSDictionary *userName = [self.friendData objectAtIndex:indexPath.row];
[[cell username] setText:[userName objectForKey:@"node_title"]];
NSDictionary *userBio = [self.friendData objectAtIndex:indexPath.row];
[[cell userDescription] setText:[userBio objectForKey:@"body"]];
NSString *profilePath = [[self.friendData objectAtIndex:indexPath.row] objectForKey:@"friendphoto"];
[cell.usermini sd_setImageWithURL:[NSURL URLWithString:profilePath]];
return cell;
} else {
sidebarCell *cell = (sidebarCell *)[tableView dequeueReusableCellWithIdentifier:@"sidebarCell"];
NSDictionary *userName = [self.neighbourData objectAtIndex:indexPath.row];
[[cell username] setText:[userName objectForKey:@"first name"]];
NSDictionary *userlast = [self.neighbourData objectAtIndex:indexPath.row];
[[cell lastName] setText:[userlast objectForKey:@"last name"]];
NSDictionary *userBio = [self.neighbourData objectAtIndex:indexPath.row];
[[cell userDescription] setText:[userBio objectForKey:@"userbio"]];
NSString *profilePath = [[self.neighbourData objectAtIndex:indexPath.row] objectForKey:@"photo_path"];
[cell.usermini sd_setImageWithURL:[NSURL URLWithString:profilePath]];
NSLog(@"This is profilePath %@",profilePath);
}
/*UITableViewCell *cell;
return cell;*/ Never put this line in your code otherwise your tableview cell its nil
}
- (IBAction)segmentControl:(id)sender {
UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;
switch (selectedSegment)
{
case 0:
[self.sidetableView setHidden:NO];
[self.neighboursView setHidden:YES];
[self.friendsView setHidden:YES];
NSLog(@"Requests!");
break;
case 1: //FRIENDS MAP VIEW
[self.sidetableView setHidden:YES];
[self.friendsView setHidden:NO];
[self.neighboursView setHidden:YES];
break;
case 2:
[self.sidetableView setHidden:YES];
[self.neighboursView setHidden:NO];
[self.friendsView setHidden:YES];
break;
}
}
当然,您的第三个tableview不能加载单元格…您错过了
返回单元格代码>在第三个IF语句中…尝试以下方法
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int numberOfRows; // this variable for return number of cell return
// here check your table
if (tableView == self.sidetableView) {
numberOfRows = [self.myFriendData count];
}else if (tableView == self.friendsView) {
numberOfRows = [self.friendData count];
}else if (tableView == self.neighboursView) {
numberOfRows = [self.neighbourData count];
}else {
numberOfRows = 0;
}
return numberOfRows;
}
回答编辑您的代码
ViewController.h
- (IBAction)segmentControl:(id)sender;
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentControl;
@property (weak, nonatomic) IBOutlet UITableView *sidetableView;
@property (strong, nonatomic) NSMutableArray *myFriendData;
@property (weak, nonatomic) IBOutlet UITableView *neighboursView;
@property (strong, nonatomic) NSMutableArray *friendData;
@property (strong, retain) NSMutableArray *neighbourData;
@property (strong, nonatomic) IBOutlet UITableView *friendsView;
- (IBAction)segmentControl:(id)sender;
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentControl;
@property (weak, nonatomic) IBOutlet UITableView *sidetableView;
@property (strong, nonatomic) NSMutableArray *myFriendData;
@property (weak, nonatomic) IBOutlet UITableView *neighboursView;
@property (strong, nonatomic) NSMutableArray *friendData;
@property (strong, retain) NSMutableArray *neighbourData;
我更喜欢使用弱
引用,而不是强
@property (weak, nonatomic) IBOutlet UITableView *friendsView;
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
UINib *nib = [UINib nibWithNibName:@"sidebarCell" bundle:nil];
[self.friendsView registerNib:nib forCellReuseIdentifier: @"sidebarCell"];
nib = [UINib nibWithNibName:@"MyFriendTableViewCell" bundle:nil];
[self.sidetableView registerNib:nib forCellReuseIdentifier: @"MyFriendTableViewCell"];
nib = [UINib nibWithNibName:@"sidebarCell" bundle:nil];
[self.neighboursView registerNib:nib forCellReuseIdentifier: @"sidebarCell"];
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.sidetableView) {
if ([self.myFriendData count] > 0) {
return [self.myFriendData count];
} else {
return 0;
}
}
if (tableView == self.friendsView) {
if ([self.friendData count] > 0) {
return [self.friendData count];
} else {
return 0;
}
}
else {
// return [self.neighbourData count];
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView == self.sidetableView) {
self.sidetableView.separatorStyle = UITableViewCellSeparatorStyleNone;
MyFriendTableViewCell *cell = (MyFriendTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"MyFriendTableViewCell"];
NSDictionary *friendsName = [self.myFriendData objectAtIndex:indexPath.row];
[[cell friendName] setText:[friendsName objectForKey:@"title"]];
NSDictionary *friendsBio = [self.myFriendData objectAtIndex:indexPath.row];
[[cell friendBio] setText:[friendsBio objectForKey:@"field_friendbio"][@"und"][0][@"safe_value"]];
NSString *profilePath = [[self.myFriendData objectAtIndex:indexPath.row] objectForKey:@"field_picture"][@"und"][0][@"safe_value"];
[cell.friendPic sd_setImageWithURL:[NSURL URLWithString:profilePath]];
NSLog(@"This is profilePath %@",profilePath);
return cell;
}
if (tableView == self.friendsView)
{
self.friendsView.separatorStyle = UITableViewCellSeparatorStyleNone;
sidebarCell *cell = (sidebarCell *)[tableView dequeueReusableCellWithIdentifier:@"sidebarCell"];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"sidebarCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
NSDictionary *userName = [self.friendData objectAtIndex:indexPath.row];
[[cell username] setText:[userName objectForKey:@"node_title"]];
NSDictionary *userBio = [self.friendData objectAtIndex:indexPath.row];
[[cell userDescription] setText:[userBio objectForKey:@"body"]];
NSString *profilePath = [[self.friendData objectAtIndex:indexPath.row] objectForKey:@"friendphoto"];
[cell.usermini sd_setImageWithURL:[NSURL URLWithString:profilePath]];
return cell;
} else {
sidebarCell *cell = (sidebarCell *)[tableView dequeueReusableCellWithIdentifier:@"sidebarCell"];
NSDictionary *userName = [self.neighbourData objectAtIndex:indexPath.row];
[[cell username] setText:[userName objectForKey:@"first name"]];
NSDictionary *userlast = [self.neighbourData objectAtIndex:indexPath.row];
[[cell lastName] setText:[userlast objectForKey:@"last name"]];
NSDictionary *userBio = [self.neighbourData objectAtIndex:indexPath.row];
[[cell userDescription] setText:[userBio objectForKey:@"userbio"]];
NSString *profilePath = [[self.neighbourData objectAtIndex:indexPath.row] objectForKey:@"photo_path"];
[cell.usermini sd_setImageWithURL:[NSURL URLWithString:profilePath]];
NSLog(@"This is profilePath %@",profilePath);
}
/*UITableViewCell *cell;
return cell;*/ Never put this line in your code otherwise your tableview cell its nil
}
- (IBAction)segmentControl:(id)sender {
UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;
switch (selectedSegment)
{
case 0:
[self.sidetableView setHidden:NO];
[self.neighboursView setHidden:YES];
[self.friendsView setHidden:YES];
NSLog(@"Requests!");
break;
case 1: //FRIENDS MAP VIEW
[self.sidetableView setHidden:YES];
[self.friendsView setHidden:NO];
[self.neighboursView setHidden:YES];
break;
case 2:
[self.sidetableView setHidden:YES];
[self.neighboursView setHidden:NO];
[self.friendsView setHidden:YES];
break;
}
}
我想您可能在numberOfRowsInSection
中没有返回数组的计数。我注意到另一个缺少的东西,您没有在cellforrowatinexpath
中为您的邻居视图返回cell
- (void)viewDidLoad {
[super viewDidLoad];
//remove un-necessary code
UINib *nib = [UINib nibWithNibName:@"sidebarCell" bundle:nil];
[self.friendsView registerNib:nib forCellReuseIdentifier: @"sidebarCell"];
[self.neighboursView registerNib:nib forCellReuseIdentifier: @"sidebarCell"];
nib = [UINib nibWithNibName:@"MyFriendTableViewCell" bundle:nil];
[self.sidetableView registerNib:nib forCellReuseIdentifier: @"MyFriendTableViewCell"];
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.sidetableView) {
if ([self.myFriendData count] > 0) {
return [self.myFriendData count];
} else {
return 0;
}
}
if (tableView == self.friendsView) {
if ([self.friendData count] > 0) {
return [self.friendData count];
} else {
return 0;
}
}
else {
//Are you forgetting un-commenting this??
// return [self.neighbourData count];
return [self.neighbourData count];
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView == self.sidetableView) {
self.sidetableView.separatorStyle = UITableViewCellSeparatorStyleNone;
MyFriendTableViewCell *cell = (MyFriendTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"MyFriendTableViewCell"];
NSDictionary *friendsName = [self.myFriendData objectAtIndex:indexPath.row];
[[cell friendName] setText:[friendsName objectForKey:@"title"]];
NSDictionary *friendsBio = [self.myFriendData objectAtIndex:indexPath.row];
[[cell friendBio] setText:[friendsBio objectForKey:@"field_friendbio"][@"und"][0][@"safe_value"]];
NSString *profilePath = [[self.myFriendData objectAtIndex:indexPath.row] objectForKey:@"field_picture"][@"und"][0][@"safe_value"];
[cell.friendPic sd_setImageWithURL:[NSURL URLWithString:profilePath]];
NSLog(@"This is profilePath %@",profilePath);
return cell;
}
if (tableView == self.friendsView)
{
self.friendsView.separatorStyle = UITableViewCellSeparatorStyleNone;
sidebarCell *cell = (sidebarCell *)[tableView dequeueReusableCellWithIdentifier:@"sidebarCell"];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"sidebarCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
NSDictionary *userName = [self.friendData objectAtIndex:indexPath.row];
[[cell username] setText:[userName objectForKey:@"node_title"]];
NSDictionary *userBio = [self.friendData objectAtIndex:indexPath.row];
[[cell userDescription] setText:[userBio objectForKey:@"body"]];
NSString *profilePath = [[self.friendData objectAtIndex:indexPath.row] objectForKey:@"friendphoto"];
[cell.usermini sd_setImageWithURL:[NSURL URLWithString:profilePath]];
return cell;
} else {
sidebarCell *cell = (sidebarCell *)[tableView dequeueReusableCellWithIdentifier:@"sidebarCell"];
NSDictionary *userName = [self.neighbourData objectAtIndex:indexPath.row];
[[cell username] setText:[userName objectForKey:@"first name"]];
NSDictionary *userlast = [self.neighbourData objectAtIndex:indexPath.row];
[[cell lastName] setText:[userlast objectForKey:@"last name"]];
NSDictionary *userBio = [self.neighbourData objectAtIndex:indexPath.row];
[[cell userDescription] setText:[userBio objectForKey:@"userbio"]];
NSString *profilePath = [[self.neighbourData objectAtIndex:indexPath.row] objectForKey:@"photo_path"];
[cell.usermini sd_setImageWithURL:[NSURL URLWithString:profilePath]];
NSLog(@"This is profilePath %@",profilePath);
// are you forgetting to return cell?
return cell;
}
}
- (IBAction)segmentControl:(id)sender {
UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;
switch (selectedSegment)
{
case 0:
[self.sidetableView setHidden:NO];
[self.neighboursView setHidden:YES];
[self.friendsView setHidden:YES];
NSLog(@"Requests!");
break;
case 1: //FRIENDS MAP VIEW
[self.sidetableView setHidden:YES];
[self.friendsView setHidden:NO];
[self.neighboursView setHidden:YES];
break;
case 2:
[self.sidetableView setHidden:YES];
[self.neighboursView setHidden:NO];
[self.friendsView setHidden:YES];
break;
}
}
我在我的第三个if语句中添加了返回单元格,但仍然得到一个空白的tableView:/为什么你发布了两个答案?请删除一行。在cellforrowatindexpath方法中添加这些行。else部分sidebarCell*单元格=(sidebarCell*)[tableView dequeueReusableCellWithIdentifier:@“sidebarCell]”;NSArray*nib=[[NSBundle mainBundle]loadNibNamed:@“sidebarCell”所有者:自选项:nil];单元格=[nib objectAtIndex:0];UINib*nib=[UINib-nibWithNibName:@“sidebarCell”bundle:nil];[self.friendsView registerNib:nib forCellReuseIdentifier:@“sidebarCell”];UINib*nib1=[UINib-nibWithNibName:@“MyFriendTableViewCell”包:无];[self.sidetableView注册表项nb:nib1强制重用标识符:@“MyFriendTableViewCell”];UINib*nib2=[UINib-nibWithNibName:@“sidebarCell”bundle:nil];[self.neighboursView注册表项nb:nib2强制重用标识符:@“sidebarCell”];更改此选项并运行