Dario Rusignuolo
Dario Rusignuolo

Reputation: 2186

populate uitableview as subview of uicollectionview

I have a problem in populating a uitableview created programmatically as a subview of uicollectionview.

here's my code:

.h file

#import <UIKit/UIKit.h>

@interface TheViewController : UICollectionViewController<UITableViewDataSource>
@property (atomic,strong) IBOutlet UIBarButtonItem *plus;
-(IBAction) addTeamPressed:(id)sender;
@end

.m file

- (void)viewDidLoad
{
    [super viewDidLoad];
   NSError *jsonParsingError = nil;

    teamsView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.window.bounds.size.width, self.view.window.bounds.size.height)];
    NSData *t = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://..."] options:NSDataReadingMappedIfSafe error:&jsonParsingError];

    NSArray *tmp = [NSJSONSerialization JSONObjectWithData:t options:0 error:&jsonParsingError];
    UITableViewCell * teamCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    teams = [[NSMutableArray alloc]init];
    [teamsView addSubview:teamCell];

teamsView.delegate = self;

teamsView.dataSource = self;
    for(int i =0;i<tmp.count;i++){
        [teams addObject:[tmp objectAtIndex:i]];
    }
    plus.title = @"Done";

    [self.view addSubview:teamsView];

}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    // If You have only one(1) section, return 1, otherwise you must handle sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [teams count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    TeamsNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = [[SquadreNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.name.text = [NSString stringWithFormat:@"%@",[[teams objectAtIndex:indexPath.row] objectForKey:@"name"]];
    NSLog(@"%d teams",[teams count]);
    return cell;
}

my tableview is displaying correctly, of course thanks to other functions not mentioned here, but no rows in the subview. the compiler prints out the number of teams, so it calls the delegate methods, but it doesn't populate the subview...

what 's wrong with this?

thanks in advance Dario

Upvotes: 0

Views: 1210

Answers (2)

Dario Rusignuolo
Dario Rusignuolo

Reputation: 2186

solution is,

    static NSString *CellIdentifier = @"Cell";

    TeamsNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell) {
        cell = [[SquadreNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }


    cell.labelText.text = [NSString stringWithFormat:@"%@",[[teams objectAtIndex:indexPath.row] objectForKey:@"name"]];
    NSLog(@"%d teams",[teams count]);
    return cell;

Upvotes: 0

Ezeki
Ezeki

Reputation: 1519

First you should remove this from your viewDidLoad:

UITableViewCell * teamCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
[teamsView addSubview:teamCell];

Then make sure that your teams array is retained, just put this line to the header file:

@property (nonatomic, strong) NSMutableArray* teams;

And then use self.teams instead of just teams

At the end of viewDidLoad method you should call [teamsView reloadData];

Also it would be good to change your table data source method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    TeamsNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell) {
        // please make sure that you allocate required object here
        cell = [[TeamsNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // please make sure that you do have `name` property in your `TeamsNameCell` class
    cell.name.text = [NSString stringWithFormat:@"%@",[[teams objectAtIndex:indexPath.row] objectForKey:@"name"]];
    NSLog(@"%d teams",[teams count]);
    return cell;

}

Please make sure that your TeamsNameCell class has name property in the header:

@property(nonatomic, strong) UILabel* name;

And also do not forget to allocate this label in the constructor of TeamsNameCell:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.name = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,50)];
        [self addSubview:self.name];
    }
    return self;
}

Or Maybe it is better to use default cell's label:

instead of cell.name.text = @"text"; use cell.textLabel.text = @"text";

Upvotes: 2

Related Questions