0

配列が必要なセクションに設定されていないという問題があります。以下のコードで:

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

2 番目の配列のみがセクションに設定されますが、他の 2 つのセクションで同じことを繰り返し、残りの配列で他のセクションを設定しません。

私が持っている別のもの-配列の1つ(3番目のものとしましょう)に最初と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;
}
4

2 に答える 2

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

あなたは私を愛しています:)

于 2011-11-27T11:23:30.843 に答える
0

問題は、TableView がデリゲート メソッドを呼び出す方法が定義されていないことです。コードでは、このセクションのtableView:cellForRowAtIndexPath:後にのみ各セクションに対して呼び出される場合にのみ機能します。tableView:numberOfRowsInSection

すべてのセクションのデータに同時にアクセスできるすべてを保存すると、問題は解決されます。たとえば、3 つの異なる配列 (セクションごとに 1 つ) を使用したり、アイテムの格納に配列の配列を使用したりできます。

于 2011-11-27T10:25:21.803 に答える