xcode iPhone SDK:如何在 UITableView 上创建部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14421188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iPhone SDK: How To Create Sections On UITableView
提问by jaytrixz
I have this data that I need to put inside my UITableView
but I'm getting confused on how to properly implement it. I cannot properly separate the values to segregate the Boys data to the Girls data.
我有这些数据需要放入我的数据中,UITableView
但我对如何正确实施它感到困惑。我无法正确分离值以将男孩数据隔离到女孩数据。
{
"QUERY": {
"COLUMNS": [
"NAME",
"GENDER"
],
"DATA": [
[
"Anne",
"Girl"
],
[
"Alex",
"Boy"
],
[
"Vince",
"Boy"
],
[
"Hyman",
"Boy"
],
[
"Shiela",
"Girl"
],
[
"Stacy",
"Girl"
]
]
},
"TOTALROWCOUNT": 6
}
I have this codes:
我有这个代码:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [genderArray count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [genderArray objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [namesArray count];
}
namesArray has all the values returned by NAME while genderArray has all the values of GENDER. I'm getting confused.
namesArray 具有 NAME 返回的所有值,而genderArray 具有 GENDER 的所有值。我越来越糊涂了。
回答by foundry
AS you are getting confused, break your data into pieces. You want two arrays, one per section. So you want one array of boys names, and another array of girls names.
当您感到困惑时,请将您的数据分成几部分。您需要两个数组,每个部分一个。所以你想要一个男孩名字数组,另一个女孩名字数组。
You can obtain this by iterating through the embedded DATA array.
您可以通过迭代嵌入的 DATA 数组来获得它。
Turn your data into an NSDictionary object. Your data looks like JSON so...
将您的数据转换为 NSDictionary 对象。您的数据看起来像 JSON 所以...
NSDictionary* myDict = [NSJSONSerialization JSONObjectWithData:myJsonData
options:0 error:&error];
Extract the data...
提取数据...
NSArray* dataArray = [myDict objectForKey:@"DATA"];
Iterate...
迭代...
NSMutableArray* boys = [[NSMutableArray alloc] init];
NSMutableArray* girls = [[NSMutableArray alloc] init];
for (id person in dataArray) {
if ([[person objectAtIndex:1] isEqualToString:@"Girl"])
[girls addObject:[person objectAtIndex:0]];
else [boys addObject:[person objectAtIndex:0]];
}
Now you have two arrays, one for each of your table sections. Make an array of sections, and put these arrays into it:
现在您有两个数组,每个表部分一个。创建一个节数组,并将这些数组放入其中:
NSArray* sections = [NSArray arrayWithObjects:boys,girls,nil];
Make a separate array for your section headers:
为您的部分标题制作一个单独的数组:
NSArray* headers = [NSArray arrayWithObjects:@"Boys",@"Girls",nil];
Now your data source methods look like this:
现在您的数据源方法如下所示:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [sections count];
}
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section
{
return [headers objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [[sections objectAtIndex:section] count];
}
and finally
最后
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
...
cell.textLabel.text = (NSString*)[[self.sections objectAtIndex:indexPath.section]
objectAtIndex:indexPath.row];