2012-04-16 14 views
0

私はUITableViewとセクション/行にいくつか問題があります。 セクション名、すべての行名、セクションごとの行数をxmlから解析します。セクションと行を動的にUITableViewに埋め込む方法は?

私は3 NSMutableArraysています(すべての行名を持つ)

nameArray sectionArray(すべてのセクション名) secCountArray(セクションごとの行数)

仕事にcellForRowAtindexPathのためには、私は返す必要がありません表示されたセクションの行? 次のステップは、セクションと各セクションのすべての行で2次元配列を作成することです。

もっと良い解決方法を知っている人はいますか?

は、ここでコードが来る:

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier] autorelease]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
} 

// Set up the cell 
int xmlEntryIndex = [indexPath indexAtPosition: [indexPath length] -1]; 

//??? 
cell.textLabel.text = [[theParser.nameArray objectAtIndex: 1]valueForKey:@"name"]; 


return cell; 
} 

ありがとう!

答えて

2

テーブルビュー全体に対して1つの配列を持つことができます。配列には、すべてのセクションの配列が含まれます。私はこれがあなたの問題のお手伝いを願ってい

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    [[cell textLabel] setText: [[[myArray objectAtIndex: indexPath.section] objectAtIndex: indexPath.row]]; 
    return cell; 
} 

:次にcellForRowAtIndexPathは次のようになります。

編集:現代のObjective-CとARCで、私はあなたが全体のテーブルビューに1つの_sectionアレイと1 _ROW配列を持つことができ

- (void)viewDidLoad { 
    .... 
    [self.tableView registerClass:[MyCellClass class] forCellReuseIdentifier:kMyCellIdentifier]; 
} 
... 
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {  

    MyCellClass *cell = [tableView dequeueReusableCellWithIdentifier:kMyCellIdentifier forIndexPath:indexPath]; 

    cell.textLabel.text = myArray[indexPath.section][indexPath.row]; 

    return cell; 
} 
+0

おかげで、これをしようとします。そして例のコードは+1です! – Nico

+0

私は間違いなく私を助けてくれました。優秀な例! –

0

としてこれを書きます。あなたのビューcontroller.hファイルの宣言配列のよう

NSMutableArray *arrSection, *arrRow; 

、コード下のビューcontroller.mファイルペースト..

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
arrSection = [[NSMutableArray alloc] initWithObjects:@"section1", @"section2", @"section3", @"section4", nil]; 
    arrRow = [[NSMutableArray alloc] init]; 
[arrSection enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
     NSMutableArray *tempSection = [[NSMutableArray alloc] init]; 
     [arrSection enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
      [tempSection addObject:[NSString stringWithFormat:@"row%d", idx]]; 
     }]; 
     [arrRow addObject:tempSection]; 
    }]; 
    NSLog(@"arrRow:%@", arrRow); 
} 

#pragma mark - Table view data source 

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

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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [[arrRow objectAtIndex:section] count]; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:nil]; 
    if(!cell) 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil]; 
    cell.textLabel.text = [[arrRow objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]]; 
    return cell; 
} 

この方法は、定義し、独自のカスタムを作成含みませんビュー。 iOS 6以降では、

- (void)tableView:(UITableView *)tableView 
    willDisplayHeaderView:(UIView *)view 
    forSection:(NSInteger)section 

代表メソッドを定義することで、背景色とテキストの色を簡単に変更できます。例えば

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section 
{ 
    // Background color 
    view.tintColor = [UIColor blackColor]; 

    // Text Color 
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view; 
    [header.textLabel setTextColor:[UIColor whiteColor]]; 

    // Another way to set the background color 
    // Note: does not preserve gradient effect of original header 
    // header.contentView.backgroundColor = [UIColor blackColor]; 
} 

あなたが動的セクションを表示し、 行見ることができますが、あなたのために役立つかもしれない。..

関連する問題