我试图在一个ViewController中放置TableViews。我在导航栏中的UISegmentControl在它们之间切换。例如。如果一个人正在显示,另一个人则被隐藏也就是说,我想为self.tableView显示一种类型的单元格和数据,为self.friendsView显示具有不同数据的不同单元格。
第一个self.tableView无缝填充,但self.friendsView似乎并没有加载其自定义单元格(尽管数据来自控制台)。我正确构建以下内容吗?如果没有,我该如何纠正?
·H
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *myFriendData;
@property (strong, nonatomic) NSMutableArray *friendData;
@property (strong, nonatomic) IBOutlet UITableView *friendsView;
的.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UINib *nib = [UINib nibWithNibName:@"sidebarCell" bundle:nil];
[self.sidetableView registerNib:nib forCellReuseIdentifier: @"sidebarCell"];
//to register next cell
nib = [UINib nibWithNibName:@"MyFriendTableViewCell" bundle:nil];
[self.friendsView registerNib:nib forCellReuseIdentifier: @"MyFriendTableViewCell"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.sidetableView) {
/*
static NSString *NetworkTableIdentifier = @"MyFriendTableViewCell";
MyFriendTableViewCell *cell = (MyFriendTableViewCell *)[tableView dequeueReusableCellWithIdentifier:NetworkTableIdentifier];
if (tableView == self.tableView)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyFriendTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
*/
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)
{
/* static NSString *NetworkTableIdentifier = @"sidebarCell";
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
sidebarCell *cell = (sidebarCell *)[tableView dequeueReusableCellWithIdentifier:NetworkTableIdentifier];
// if (tableView == self.friendsView) */
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;
}
UITableViewCell *cell;
return cell;
}
答案 0 :(得分:3)
为每个tableview都有一个return语句。
if (tableView == self.sidetableView)
{
....
return cell;
}
if (tableView == self.friendsView)
{
....
return cell;
}
我看到的另一个错误是你正在为细胞分配两个不同的引用。 例如:
sidebarCell *cell = (sidebarCell *)[tableView dequeueReusableCellWithIdentifier:NetworkTableIdentifier];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"sidebarCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
不要这样做。如果你从xib加载单元格,frist在viewDidLoad中注册它。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UINib *nib = [UINib nibWithNibName:@"sidebarCell" bundle:nil];
[self.sidetableView registerNib:nib forCellReuseIdentifier: @"sidebarCell"];
//to register next cell
nib = [UINib nibWithNibName:@"MyFriendTableViewCell" bundle:nil];
[self. friendsView registerNib:nib forCellReuseIdentifier: @"MyFriendTableViewCell"];
}
并在cellForRow方法中,将其命名为:
仅供参考:班级名称始终以大写字母
开头 SidebarCell *cell = (SidebarCell *)[tableView dequeueReusableCellWithIdentifier:@"sidebarCell"];
答案 1 :(得分:0)
而不是将xib添加为单元格,您可以直接使用表格视图的自定义视图。任何方式查看它是否有效。
//做这样的事。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *myCell;
if (tableView == self.sidetableView) {
/*
static NSString *NetworkTableIdentifier = @"MyFriendTableViewCell";
MyFriendTableViewCell *cell = (MyFriendTableViewCell *)[tableView dequeueReusableCellWithIdentifier:NetworkTableIdentifier];
if (tableView == self.tableView)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyFriendTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
*/
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);
myCell = cell
}
else if (tableView == self.friendsView)
{
/* static NSString *NetworkTableIdentifier = @"sidebarCell";
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
sidebarCell *cell = (sidebarCell *)[tableView dequeueReusableCellWithIdentifier:NetworkTableIdentifier];
// if (tableView == self.friendsView) */
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]];
myCell = cell;
}
return myCell;
}