views:

1819

answers:

3

Hey guys i've got an problem and don't know how to handle it.

if i make a section the error occurs.

Heres my code it would be so nice if somewone can help me :)

I've already tried to set the section index to 0 and 1 but that didn't help either.

Thanks in advance

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

static NSString *CellIdentifier = @"Cell";
static NSString *CellIdentifier1 = @"Cell1";


if(indexPath.section == 1) {
 if(indexPath.row == 0) {

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
 if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
 cell.text = @"test 1";

 }

 return cell;
}
else if(indexPath.row == 1) {
 UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
 if (cell1 == nil) {
  cell1 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
  cell1.text = @"test 2";

 }

 return cell1;
}
}

else if(indexPath.section == 2) { if(indexPath.row == 0) {

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
 if (cell == nil) {
  cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
  cell.text = @"test 1";

 }

 return cell;
}
else if(indexPath.row == 1) {

UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; if (cell1 == nil) { cell1 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease]; cell1.text = @"test 2";

 }

 return cell1;
}

} }

A: 

Sections and rows start at 0. As far as I can see, you don't return a cell for indexPath with section 0, row 0.

Edit: re-posting the source code (more readable):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    static NSString *CellIdentifier1 = @"Cell1";
    if(indexPath.section == 1) {
        if(indexPath.row == 0) {
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) {
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
       cell.text = @"test 1";
      }
      return cell;
     }
     else if(indexPath.row == 1) {
      UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
      if (cell1 == nil) {
                cell1 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
                cell1.text = @"test 2";
      }
      return cell1;
     }
    }
    else if(indexPath.section == 2) {
     if(indexPath.row == 0) {
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) {
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
       cell.text = @"test 1";
      }
      return cell;
     }
     else if(indexPath.row == 1) {
      UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
      if (cell1 == nil) {
       cell1 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
       cell1.text = @"test 2";
      }
      return cell1;
     }
    }
}
drvdijk
sections start at 1 actually. the function `-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView` should always return at least '1' not '0' .. that might also cause a crash.
MiRAGe
There indeed always is 1 section, but the number of the first section in the indexpath is zero right? indexPath.section == 0 -> first section
drvdijk
+1 - also, the compiler should have (and probably did) warn about the fact that one of the paths through the (non void) function doesn't return anything.
frankodwyer
A: 

Basic question. Have u updated the number of sections in - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section. Normal obj msg send error.. comes when you are acessing something you have released or something which you do not own.. for example... in the init method of the table view you write this code sectionArray =[NSArray arraywithcontentsoffile:xyz.plist]

and in the numberofRowsInSection you use something like this [sectionarray count];

wherein sectionArray is an instance variable..

A: 

Thanks for your replies!

I've wrote the whole code once more and run it every time i added a line of code. That helped. I think the problem was the if(indexPath.section == 0) and so on. I only set the content for 2 of the 3 sections.

But i'm not sure wheather this was the problem.

Thanks for your great help!

As a beginner objective c is not that easy :)

johnlikesit