2011-11-27 52 views
0

我有一個問題,我的數組未設置爲我想要的部分。 在下的代碼:部分中的數組問題(TableView)

  • (NSInteger的)的tableView:(UITableView的*)的tableView numberOfRowsInSection:(NSInteger的)部分

只有所述第二陣列被設置成部分,但它重複在其自其他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

首先,您應該在委託方法之外創建和存儲數據。在-(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; 
} 

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

+0

正在加載中,請稍候...正在加載... לגביהקוד,סידרתיאותובדיוקכמושרשמתועםאותםשמותמשתניםאבלזהנותןלישתישגיאותבמקומותהבאים: - (NSInteger的)的tableView:(UITableView的*)的tableView numberOfRowsInSection:(NSInteger的)部分{ 的NSArray * dataArray中= [self.data objectAtIndex:indexPath.section]; return [dataArray count]; 使用說明: 使用未聲明的標識符'indexpath' –

+0

已有保密聲明使用條款隱私聲明 המוןהמוןתולהעלהעזרה! –

+0

關於你的問題,這是我的錯誤...該代碼是由我在瀏覽器中寫道和XCode中沒有被檢查。 應該有: - (NSInteger的)的tableView:(UITableView的*)的tableView numberOfRowsInSection:(NSInteger的)部分{ 的NSArray * dataArray中= [self.data objectAtIndex:節]。 返回[dataArray中計數]; } – Ariel

0

的問題是,它沒有規定怎樣的TableView會打電話給你的委託方法。在您的代碼中,只有在本節中僅在tableView:numberOfRowsInSection之後纔會爲每個部分調用tableView:cellForRowAtIndexPath:,它纔會起作用。

如果您同時存儲所有部分的數據,您的問題將得到解決。例如,您可以使用3個不同的數組(每個部分一個)或使用數組數組來存儲項目。

+0

丹尼斯感謝您的幫助,但可以ü給我一個你寫的東西的例子? –

+0

請參閱@ Ariel的回答,他如何初始化self.data數組 – Denis