我有一个问题,我的数组未设置为我想要的部分。在下面的代码中:
- (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;
}