部分中的数组问题(TableView)

时间:2011-11-27 10:07:18

标签: ios uitableview sections

我有一个问题,我的数组没有设置为我想要的部分。 在下面的代码中:

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

只有第二个数组被设置到该部分中,但它在其他2个部分上重复自身,而不是将其他部分与其余数组一起设置。

我有一个东西 - 如果我放入其中一个数组(比如第三个)超过5行,如第一个和第二个数组,它会给我错误:由于未捕获的异常'NSRangeException'终止应用程序,原因: ' * - [__ NSArrayM objectAtIndex:]:索引5超出界限[0 .. 4]'

我做错了什么? 谢谢你的帮助!

代码:

(sectionArray和dataArray是我的.h文件中的NSMutableArray变量)

- (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;
}

2 个答案:

答案 0 :(得分:2)

首先,您应该在委托方法之外创建和存储数据。在-(void)viewDidLoad;-(id)init;中,实际上并不重要。重要的是,您的数据数组(或数组或数组数组)应该在委托调用之前创建,并且在表视图调用它的数据委托时应该保持一致。

让我们说一些viewController.h

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

和相应的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;
}

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

答案 1 :(得分:0)

问题是,没有定义TableView如何调用委托方法。在您的代码中,只有在此部分tableView:cellForRowAtIndexPath:之后才会为每个部分调用tableView:numberOfRowsInSection时,它才有效。

如果您同时存储了所有部分的数据访问权限,那么您的问题将得到解决。例如,您可以使用3个不同的数组(每个部分一个)或使用数组数组来存储项目。

相关问题