0

I want to create one TableView that has 2 section rows. this table has 2 section (first section has 1 cell and second section has 3 cell)

notice:cell of first section different with cells of second section.

this is my code but don't working!!!

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

    NSArray *name = [_names objectForKey:key];
    static NSString *CellIdentifier2 = @"CustomerCell";

    if (indexPath.section == 0) {

        static NSString *CellIdentifier = @"myCell";
        FirstCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[FirstCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        // Configure the cell...
        cell.nameLable.text = [NSString stringWithFormat:@"blue"];
        cell.numberLable.text = [NSString stringWithFormat:@"1212432543"];
        cell.profileImage.image = [UIImage imageNamed: @"profile.png"];

        return cell;
//this part not working !!! XD

    }
    else if (indexPath.section >= 1) {

        CustomerCell *cell = (CustomerCell *)[self.table dequeueReusableCellWithIdentifier:CellIdentifier2];
        if (cell == nil)
        {
            NSArray *topLevelObject = [[NSBundle mainBundle] loadNibNamed:@"CustomerCell" owner:nil options:nil];

            for (id currentObject in topLevelObject) {
                if ([currentObject isKindOfClass:[CustomerCell class]]) {
                    cell = (CustomerCell *)currentObject;
                    break;
                }
            }
        }
        // Configure the cell...
        cell.titleLable.text = [name objectAtIndex:indexPath.row];
        return cell;
    }
    return nil;
}

customerCell & FirstCell are tow UITableViewCell for custom cells. when I to do run this code only section one not working and don't show cell but another section is working please guide me and tell me where is it my mistake.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
MmD
  • 2,044
  • 5
  • 22
  • 41
  • I think problem is here: " cell = [[FirstCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];" you should load nib like you have done for second section. – Pooja M. Bohora Apr 07 '14 at 11:40
  • @PoojaM.Bohora Its not need to load a nib, it can be a custom class for `UITableViewCell`. – iphonic Apr 07 '14 at 11:43
  • 1
    @iphonic: He is not using its xib while creating the firstcell. Asper his implementation that was just a guess. As I have not seen the full code. – Pooja M. Bohora Apr 07 '14 at 11:44
  • @PoojaM.Bohora Isn't it same as [[UITableViewCell alloc] init] where `UITableViewCell == FirstCell` ? – iphonic Apr 07 '14 at 11:45
  • @iphonic: Depends on his approach as he has created cell in different way for second section. Let him check once. – Pooja M. Bohora Apr 07 '14 at 11:46
  • `NSArray *topLevelObject = [[NSBundle mainBundle] loadNibNamed:@"CustomerCell" owner:nil options:nil];` shouldn't it be **owner:self** instead of nil ? – TheTiger Apr 07 '14 at 11:50
  • guys I so confused!!! FirstCell & customerCell have xib file and I load cell from there!!! – MmD Apr 07 '14 at 11:53
  • @mamal10 : Have you resolved your issue or not? – Pooja M. Bohora Apr 07 '14 at 11:53
  • I cannot resolve my issue. – MmD Apr 07 '14 at 11:55
  • 1
    @mamal10: try this FirstCell *cell = (FirstCell *)[tableView dequeueReusableCellWithIdentifier:strEvalIdentifier]; if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FirstCell" owner:self options:nil]; cell = [nib objectAtIndex:0]; } – Pooja M. Bohora Apr 07 '14 at 11:56
  • please tell me how to use 2 TableViewCell in one tableview – MmD Apr 07 '14 at 12:11

2 Answers2

1

Try this:

FirstCell *cell = (FirstCell *)[tableView dequeueReusableCellWithIdentifier:strEvalIdentifier]; 
if (cell == nil)
{
   NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FirstCell" owner:self options:nil];
   cell = [nib objectAtIndex:0];
} 
Pooja M. Bohora
  • 1,311
  • 1
  • 14
  • 42
0

Try using the same code as in the second cell.

if (indexPath.section == 0) {
FirstCell *cell = (FirstCell *)[self.table dequeueReusableCellWithIdentifier:CellIdentifier2];
    if (cell == nil)
    {
        NSArray *topLevelObject = [[NSBundle mainBundle] loadNibNamed:@"FirstCell" owner:nil options:nil];

        for (id currentObject in topLevelObject) {
            if ([currentObject isKindOfClass:[FirstCell class]]) {
                cell = (FirstCell *)currentObject;
                break;
            }
        }
// Configure the cell...
    cell.nameLable.text = [NSString stringWithFormat:@"blue"];
    cell.numberLable.text = [NSString stringWithFormat:@"1212432543"];
    cell.profileImage.image = [UIImage imageNamed: @"profile.png"];

    return cell;
    }
borncrazy
  • 1,589
  • 1
  • 12
  • 9