Reputation: 1439
I'm trying to place to TableViews inside of one ViewController. My UISegmentControl in the navigation bar switches between the two of them. E.g. if one is showing, the other is hidden. That said, I want to display one type of cell and data for self.tableView, and a different cell with different data for self.friendsView.
The first one self.tableView populates seamlessly, however self.friendsView doesn't seem to load its custom cell (though the data is coming through the console). Am I structuring the below correctly? If not, how can I correct it?
.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;
}
Upvotes: 0
Views: 215
Reputation: 173
instead of adding xib as a cell you could directly use a custom view for your table view.Any ways See if it works.
// Do something like this.
- (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;
}
Upvotes: 0
Reputation: 11201
Have a return statement for each of the tableview.
if (tableView == self.sidetableView)
{
....
return cell;
}
if (tableView == self.friendsView)
{
....
return cell;
}
Another mistake I see is you are assigning two different references to the cell. For example:
sidebarCell *cell = (sidebarCell *)[tableView dequeueReusableCellWithIdentifier:NetworkTableIdentifier];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"sidebarCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
Don't do this.If you are loadin the cell from xib, frist register it in the 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"];
}
and in the cellForRow method, call it as:
FYI: class names always start with capital letter
SidebarCell *cell = (SidebarCell *)[tableView dequeueReusableCellWithIdentifier:@"sidebarCell"];
Upvotes: 3