1

我正在尝试学习如何编写 UITableView 并且在该部分的编程方面遇到了一些问题。

我已经声明了 3 个带有字符串的数组和 1 个带有 3 个数组的数组。

firstSection = [NSArray arrayWithObjects:@"Red", @"Blue", nil];
secondSection = [NSArray arrayWithObjects:@"Orange", @"Green", @"Purple", nil];
thirdSection = [NSArray arrayWithObject:@"Yellow"];

array = [[NSMutableArray alloc] initWithObjects:firstSection, secondSection, thirdSection, nil];

显示标题

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

NSString * title;
title = [NSString stringWithFormat:@"%@" , [array objectAtIndex:section]];

return title;
}

它将数组本身显示为标题

在此处输入图像描述

因此,是否可以使用数组的名称(例如 firstSection 和 secondSection)实际显示部分的名称?

4

2 回答 2

6

在您的情况下,最好将数组存储在NSDictionary. 例如,如果您声明并合成一个NSDictionary变量 calledtableContents和 an NSArray called titleOfSections,您可以执行以下操作:

 - (void)viewDidLoad {
    [super viewDidLoad];

    //These will automatically be released. You won't be needing them anymore (You'll be accessing your data through the NSDictionary variable)
    NSArray *firstSection = [NSArray arrayWithObjects:@"Red", @"Blue", nil];
    NSArray *secondSection = [NSArray arrayWithObjects:@"Orange", @"Green", @"Purple", nil];
    NSArray *thirdSection = [NSArray arrayWithObject:@"Yellow"];

    //These are the names that will appear in the section header
    self.titleOfSections = [NSArray arrayWithObjects:@"Name of your first section",@"Name of your second section",@"Name of your third section", nil];

    NSDictionary *temporaryDictionary = [[NSDictionary alloc]initWithObjectsAndKeys:firstSection,@"0",secondSection,@"1",thirdSection,@"2",nil];
    self.tableContents = temporaryDictionary;
    [temporaryDictionary release];
}

然后在表视图控制器方法中:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.titleOfSections count];
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
    return [[self.tableContents objectForKey:[NSString stringWithFormat:@"%d",section]] count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    //Setting the name of your section
    return [self.titleOfSections objectAtIndex:section]; 
}

cellForRowAtIndexPath然后在您的方法中访问每个数组的内容:

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

    NSArray *arrayForCurrentSection = [self.tableContents objectForKey:[NSString stringWithFormat:@"%d",indexPath.section]];

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] 
                 initWithStyle:UITableViewCellStyleSubtitle 
                 reuseIdentifier:SimpleTableIdentifier] autorelease];
    }
    cell.textLabel.text = [arrayForCurrentSection objectAtIndex:indexPath.row];

    return cell;
}
于 2012-04-14T18:16:54.447 回答
0

你可以这样做吗

  NSArray   *SectionArray = [[NSArray alloc]initwithArray:[array objectAtIndex:section]];

  NSString * title;

  title = [NSString stringWithFormat:@"%@" , [SectionArray objectAtIndex:section]];

   return title;
于 2012-04-14T17:13:54.527 回答