mkral
mkral

Reputation: 4085

Adding Cells to UITableView from array

I pass an array called userArray which stores another array with strings. This is what I have so far but I know it's wrong. Can someone point me in the right direction?

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

    static NSString *CellIdentifier = @"Cell";    
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil) {
        cell = [[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
    }

    //Set Up Cell
    DataSingleton *sharedData = [DataSingleton sharedData];

    for (NSArray *array in sharedData.usersArray){
        cell.primaryLabel.text = [array objectAtIndex:1];
        cell.secondaryLabel.text = [array objectAtIndex:2];
        cell.profileImage = [UIImage imageNamed:@"111-user.png"];
        return cell;
    }
}

Upvotes: 0

Views: 1862

Answers (1)

anticyclope
anticyclope

Reputation: 1587

cellForRowAtIndexPath is a UITableViewDataSource method, and it asks the data for single cell only. So, you have to remove a loop, and set up a single cell at once, using indexPath.row as an array index in your DataSingleton

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

    static NSString *CellIdentifier = @"Cell";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil) {
        cell = [[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
    }

    //Set Up Cell
    DataSingleton *sharedData = [DataSingleton sharedData];

    NSArray *array = [sharedData.usersArray objectAtIndex:indexPath.row];
    cell.primaryLabel.text = [array objectAtIndex:1];
    cell.secondaryLabel.text = [array objectAtIndex:2];
    cell.profileImage = [UIImage imageNamed:@"111-user.png"];
    return cell;   
}

Also, you should implement tableView:numberOfRowsInSection: to return [[[DataSingleton sharedData] usersArray] count]

Upvotes: 1

Related Questions