Question

I have an issue that my arrays are not set to the sections i want. In the code under:

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

Only the second array is set into the section but it repeats its self on the other 2 sections and not set the other sections with the rest of the arrays.

anoter thing i have - if i put in one of the array (let's say the third one) more then 5 rows like the first and the second array, it give me error: Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 4]'

What i'm doing wrong?? Thanks for the help!

The code:

(The sectionArray and dataArray are NSMutableArray variables in my .h file)

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
sectionArray =  [NSArray arrayWithObjects:@"section1", @"section2", @"section3", nil];

return [sectionArray count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:        (NSInteger)section
{

NSString *sectionHeader = nil;

    if(section == 0)
    {
        sectionHeader = @"אתרים חשובים";
    }

    if(section == 1)
    {
        sectionHeader = @"מתעניינים";
    }

    if(section == 2)
    {
        sectionHeader = @"טלפונים שימושיים";
    }

return sectionHeader;
}

// Returns the number of rows in a given section.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{        
    if(section == 0)
    {
        dataArray = [[NSMutableArray alloc] initWithObjects:
                     @"אתר אורנים",
                     @"כניסה למערכת ניהול שיעורים",
                     @"אלפון אנשי סגל",
                     @"אתר אגודת הסטודנטים",
                     @"מפת אורנים", nil];
    }

    if(section == 1)
    {
        dataArray = [[NSMutableArray alloc] initWithObjects:
                     @"תנאי קבלה",
                     @"שאלות נפוצות",
                     @"הרשמה אונליין",
                     @"יצירת קשר עם יועץ לימודים",
                     @"תחבורה ציבורית", nil];
    }

    if(section == 2)
    {
        dataArray = [[NSMutableArray alloc] initWithObjects:
                     @"מרכז המידע וההרשמה",
                     @"שכר לימוד",
                     @"שכר לימוד (מספר נוסף)",
                     @"קופת אורנים",
                     @"מרכז ההשאלה בספריה", nil];
                    /* 
                    @"תמיכת הספריה",
                     @"מעונות",
                     @"מכלול",
                     @"רכז בטחון",
                     @"שער",
                     @"אגודת הסטודנטים",
                     @"רדיו אורנים",
                     @"פקולטות:",
                     @"מנהל תלמידים",
                     @"מנהל תלמידים (מספר נוסף)", nil];
                      */
    }

    return [dataArray count];
}

// Returns the cell for a given indexPath.
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:    (NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    // Create the cell.
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault     reuseIdentifier:CellIdentifier];
}

cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
cell.textLabel.textAlignment = UITextAlignmentRight;

float Rvalue = (1.0 * 000) / 255;
float Gvalue = (1.0 * 139) / 255;
float Bvalue = (1.0 * 69) / 255;
cell.textLabel.textColor = [UIColor colorWithRed:Rvalue green:Gvalue blue:Bvalue   alpha:1.0f];

UIImage *cellImage = [UIImage imageNamed:@"oranimCellIco.png"];
cell.imageView.image = cellImage;

return cell;
}
Was it helpful?

Solution

First of all, you should create and store your data outside of the delegate methods. In -(void)viewDidLoad; or -(id)init;, it doesn't matter in fact. What does matter, is that your data array (or arrays or array of arrays) should be created before delegate calls and should stay consistant while table view calls it's delegate for data.

Let's say some viewController.h

@property(nonatomic, retain)NSArray data;
@property(nonatomic, retain)NSArray sections;

And the corresponding viewController.m

-(void)viewDidLoad{
    [super viewDidLoad];
    self.sections = [[NSArray alloc] initWithObjects:@"אתרים חשובים",@"מתעניינים",@"טלפונים שימושיים",nil];
    self.data = [[NSArray alloc]initWithObjects:
                 [[NSArray alloc] initWithObjects:
                 @"אתר אורנים",
                 @"כניסה למערכת ניהול שיעורים",
                 @"אלפון אנשי סגל",
                 @"אתר אגודת הסטודנטים",
                 @"מפת אורנים", nil],
                 [[NSArray alloc] initWithObjects:
                 @"תנאי קבלה",
                 @"שאלות נפוצות",
                 @"הרשמה אונליין",
                 @"יצירת קשר עם יועץ לימודים",
                 @"תחבורה ציבורית", nil],
                 [[NSArray alloc] initWithObjects:
                 @"מרכז המידע וההרשמה",
                 @"שכר לימוד",
                 @"שכר לימוד (מספר נוסף)",
                 @"קופת אורנים",
                 @"מרכז ההשאלה בספריה", nil]nil];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
     return [self.sections count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [self.sections objectAtIndex:section];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray* dataArray = [self.data objectAtIndex:indexPath.section];
    return [dataArray count];
}

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil){
        // Create the cell.
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault     reuseIdentifier:CellIdentifier];
    }

    NSArray* dataArray = [data objectAtIndex:indexPath.section];
    cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
    cell.textLabel.textAlignment = UITextAlignmentRight;

    cell.textLabel.textColor = [UIColor colorWithRed:0.0 green:139.0/255.0 blue:69.0/255.0 alpha:1.0];

    UIImage *cellImage = [UIImage imageNamed:@"oranimCellIco.png"];
    cell.imageView.image = cellImage;

    return cell;
}

קצת סדר בקוד לא יפגע אף פעם :)

OTHER TIPS

The issue is, that it's not defined how the TableView will call you delegate methods. In your code, it will only work, if tableView:cellForRowAtIndexPath: will be called for each section only after tableView:numberOfRowsInSection for this section.

Your issues will be resolved, if you store all you have access to the data for the all sections at the same time. For example you can use 3 different arrays for (one for each section) or use array of arrays for storing items.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top