2012-04-16 74 views
0

我有一些UITableView和部分/行的问题。 Iam从xml解析节的名称,每节的所有行名和行数。如何动态填充UITableView的部分和行?

我有3个NSMutableArrays:

nameArray(所有行名称) sectionArray(所有的节名) secCountArray(每节行数)

有关的cellForRowAtIndexPath工作,我必须回显示部分的行? 接下来我要做的是为每个部分构建一个包含部分和所有行的2d数组。

有谁知道更好的解决方案?

这里谈到的代码:

// 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

您可以为整个表格视图创建一个数组。该数组包含每个部分的数组。然后cellForRowAtIndexPath可能看起来像:

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

我希望这可以帮助你在你的问题。

编辑:随着现代Objective-C和ARC我会写为

- (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 2012-04-17 12:46:25

+0

它肯定帮我和我一起。杰出的例子! – 2014-03-15 06:14:48

0

你可以有一个_section阵列和一个_row阵列整个的tableview。

喜欢你的观点或者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]; 
} 

你可以看到显示的动态部分和行 可能对您有帮助..