Ios 嵌套NSArray的UITableView中的不同节标题
我正在尝试学习如何编写UITableView,但在本节的编程中遇到了一些问题 我已经声明了3个带字符串的数组和1个带3个数组的数组Ios 嵌套NSArray的UITableView中的不同节标题,ios,uitableview,Ios,Uitableview,我正在尝试学习如何编写UITableView,但在本节的编程中遇到了一些问题 我已经声明了3个带字符串的数组和1个带3个数组的数组 firstSection = [NSArray arrayWithObjects:@"Red", @"Blue", nil]; secondSection = [NSArray arrayWithObjects:@"Orange", @"Green", @"Purple", nil]; thirdSection = [NSArray arrayWithObject:
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;
}
它将数组本身显示为标头
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
NSString * title;
title = [NSString stringWithFormat:@"%@" , [array objectAtIndex:section]];
return title;
}
因此,是否可以使用数组的名称(如firstSection和secondSection)实际显示节的名称
NSArray *SectionArray = [[NSArray alloc]initwithArray:[array objectAtIndex:section]];
NSString * title;
title = [NSString stringWithFormat:@"%@" , [SectionArray objectAtIndex:section]];
return title;
你能这样做吗
NSArray *SectionArray = [[NSArray alloc]initwithArray:[array objectAtIndex:section]];
NSString * title;
title = [NSString stringWithFormat:@"%@" , [SectionArray objectAtIndex:section]];
return title;
在您的情况下,最好将数组存储在NSDictionary中。例如,如果声明并合成名为tableContents的NSDictionary变量和名为titleOfSections的NSArray,则可以执行以下操作:
- (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方法中每个数组的内容,请执行以下操作:
在您的情况下,最好将数组存储在NSDictionary中。例如,如果声明并合成名为tableContents的NSDictionary变量和名为titleOfSections的NSArray,则可以执行以下操作:
- (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方法中每个数组的内容,请执行以下操作:
我真的不明白您想要的是什么-数组的名称,例如firstSection?或者数组中的一个项目,例如红色?是的,我想显示数组的名称。我不太明白您想要的是什么-数组的名称,例如firstSection?或者数组中的一项,例如红色?是的,我想显示数组的名称。这不起作用,因为第三节只有1项,所以在节=2的情况下,索引将超出范围。我不确定wakaka是否真的明确了他想要什么…这不会起作用,因为第三节只有1项,所以在节=2的情况下,索引将超出范围。我不确定瓦卡卡是否真的说清楚了他想要什么…谢谢你的回复。我看到您创建了第三个数组,名为titleOfSections,并将名称放入其中。不显示数组本身的名称是否可能?我想这是唯一的方法。谢谢,我完全不知道有没有更好的方法。但这对我来说非常有效。谢谢@sooperThanks的回复。我看到您创建了第三个数组,名为titleOfSections,并将名称放入其中。不显示数组本身的名称是否可能?我想这是唯一的方法。谢谢,我完全不知道有没有更好的方法。但这对我来说非常有效。谢谢@sooper