tinhead
tinhead

Reputation: 385

how to return two custom uitableviewcells

Hi just trying to figure out how to load two different custom uitableviewcells into two different sections on my uitableview... Just not sure on how to proceed... here is the code I have currently

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

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

    //Registration Button
    static NSString *CellIdentifier = @"CustomRegCell";
    static NSString *CellNib = @"LogInCustomCell";

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
        cell = (UITableViewCell *)[nib objectAtIndex:0];
    }
    return cell;

}

/////// NEW ATTEMPT.... :( if you could call it that..

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

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

    if (indexPath.section == 0) 
    {
        //Registration Button
        static NSString *CellIdentifier = @"CustomRegCell";
        static NSString *CellNib = @"LogInCustomCell";

        UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
            cell = (UITableViewCell *)[nib objectAtIndex:0];
        }
        return cell;        

    }
    else if (indexPath.section == 1)
    {
        //Registration Button
        static NSString *CellButtonIdentifier = @"CustomButtonCell";
        static NSString *CellButtonNib = @"LogInCustomCell";

        UITableViewCell *cellButton = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellButtonIdentifier];
        if (cellButton == nil) {
            NSArray *nibButton = [[NSBundle mainBundle] loadNibNamed:CellButtonNib owner:self options:nil];
            cellButton = (UITableViewCell *)[nibButton objectAtIndex:0];
        }
        return cellButton;      

    }
    return nil;

}

Upvotes: 2

Views: 1585

Answers (1)

Alan Zeino
Alan Zeino

Reputation: 4396

Use the section property of the indexPath variable:

if (indexPath.section == 0) 
{
    // do this
}
else if (indexPath.section == 1)
{
    // do that
}

The section number is based on the order in which they appear. The first section you want to show in the tableView will be 0, and so on.

Upvotes: 3

Related Questions