SIGABRT and Setting Cells

Discussion in 'iOS Programming' started by Wellington2k, Jan 21, 2012.

  1. Wellington2k macrumors regular

    Joined:
    Jun 4, 2011
    #1
    Hello.

    I have a UITableView and three arrays.

    One for section 1, next for section 2, last carries both arrays.

    When I add a value to the first array I get the error SIGABRT.

    Code:
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        // Return the number of rows in the section.
        
        if(section == 0) {
            return [array1 count];
        }
        if(section == 1) {
            return [array2 count];
        }
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
    [U][B]//SIGABRT HAPPENS ON LINE BELOW[/B][/U]
        cell.textLabel.text = [NSString stringWithFormat:@"%@",[[botharrays objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]];
        cell.backgroundColor = [UIColor colorWithWhite:100 alpha:.65f];
        
        return cell;
    }
    
    PS: What is a good book to help learn Objective-C?

    Thanks!
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    You get the SIGABRT error because the nice developers at Apple want to send you a message that there is a bug in your code. They decided that you have made a programmer error. They are so nice that they print out an explanation in the debugger console with the details.

    You should look in the debugger console for their message to you.

    Errors with arrays in code like you show are usually caused by out of range errors. You've asked for an objectAtIndex: where index is past the end of the array. I recommend that you change the code to do each of the objectAtIndex calls in its own expression, that is, each one on its own line of code. This will help you to figure out the one that is failing.
     
  3. Wellington2k, Jan 21, 2012
    Last edited: Jan 21, 2012

    Wellington2k thread starter macrumors regular

    Joined:
    Jun 4, 2011
    #3
    Oops!

    I read the console and it is an out of range error.

    It's index 2.
    0-2.

    How would I fix the bounds?
     
  4. forum user macrumors regular

    Joined:
    Aug 28, 2008
    #4
    You don't fix the bounds. You should stop asking the array for an object at a position outside the bounds.

    If you have three objects in your array then these object are at position 0, 1 and 2. The bounds are from 0 to 2. If you ask you array for an object at position "three" you get the out-of-bounds error. Arrays start counting a the position zero, last used position in an array of three is therefore position two.

    In your case look at the method numberOfSections and numberOfRowsInSection. These two methods tell the tableView about the bounds of your array.
    Not sure where you get array1 and array2 from in numberOfRowsInSection. You use "botharrays" in cellForRowAtIndexPath. Why not use it in numberOfRowsInSection, too?

    Code:
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [[botharrays objectAtIndex:section] count];
    }
    
    - Olaf
     
  5. Wellington2k thread starter macrumors regular

    Joined:
    Jun 4, 2011
    #5
    Thanks for the code snippet above!

    But I found the error was that I had set numberofsections to the count of array1 instead of headerarray.

    Thank you for your help!
     
  6. forum user macrumors regular

    Joined:
    Aug 28, 2008

Share This Page