iPhone编程表
我在我的应用程序中添加了一个表格,并通过该表格添加了一个滚动条,就像 iPhone 中的联系人一样。卷轴正在处理字母,但我有一个问题,在字母 A 下,我得到字母 A 的名称,但在字母 B 中我得到与字母 A 相同的名称。 列表 A 的数组的 .plist 中的相同单词在所有其余列表中重复。 我能做些什么??
注意:添加您自己的 .plist 文件以查看它给出的结果。
#import "SimpleTableViewController.h"
@implementation SimpleTableViewController
@synthesize exercises;
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
if(searching)
return nil;
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
[tempArray addObject:@"A"];
[tempArray addObject:@"B"];
[tempArray addObject:@"C"];
[tempArray addObject:@"D"];
[tempArray addObject:@"E"];
[tempArray addObject:@"F"];
[tempArray addObject:@"G"];
[tempArray addObject:@"H"];
[tempArray addObject:@"I"];
[tempArray addObject:@"J"];
[tempArray addObject:@"K"];
[tempArray addObject:@"L"];
[tempArray addObject:@"M"];
[tempArray addObject:@"N"];
[tempArray addObject:@"O"];
[tempArray addObject:@"P"];
[tempArray addObject:@"Q"];
[tempArray addObject:@"R"];
[tempArray addObject:@"S"];
[tempArray addObject:@"T"];
[tempArray addObject:@"U"];
[tempArray addObject:@"V"];
[tempArray addObject:@"W"];
[tempArray addObject:@"X"];
[tempArray addObject:@"Y"];
[tempArray addObject:@"Z"];
return tempArray;
}
- (void)viewDidLoad {
NSString *myFile = [[NSBundle mainBundle] pathForResource:@"A" ofType:@"plist"];
exercises = [[NSArray alloc]initWithContentsOfFile:myFile];
[super viewDidLoad];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section ==0) return 5;
if (section ==1) return 2;
if (section ==2) return 5;
if (section ==3) return 5;
if (section ==4) return 6;
if (section ==5) return 5;
if (section ==6) return 5;
if (section ==7) return 5;
if (section ==8) return 5;
if (section ==9) return 5;
if (section ==10) return 5;
if (section ==11) return 5;
if (section ==12) return 5;
if (section ==13) return 5;
if (section ==14) return 5;
if (section ==15) return 5;
if (section ==16) return 5;
if (section ==17) return 5;
if (section ==18) return 5;
if (section ==19) return 5;
if (section ==20) return 5;
if (section ==21) return 5;
if (section ==22) return 5;
if (section ==23) return 5;
if (section ==24) return 5;
if (section ==25) return 5;
return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil)
{
cell=[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle/*Default*/// change default to subtitle
reuseIdentifier:@"cell"];
}
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
//cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
cell.textLabel.text=[exercises objectAtIndex:indexPath.row];
cell.detailTextLabel.text=@"Click me...";
return cell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 26;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:
(NSInteger)section
{
if (section == 0) return @"Letter A";
if (section == 1) return @"Letter B";
if (section == 2) return @"Letter C";
if (section == 3) return @"Letter D";
if (section == 4) return @"Letter E";
if (section == 5) return @"Letter F";
if (section == 6) return @"Letter G";
if (section == 7) return @"Letter H";
if (section == 8) return @"Letter I";
if (section == 9) return @"Letter J";
if (section == 10) return @"Letter K";
if (section == 11) return @"Letter L";
if (section == 12) return @"Letter M";
if (section == 13) return @"Letter N";
if (section == 14) return @"Letter O";
if (section == 15) return @"Letter P";
if (section == 16) return @"Letter Q";
if (section == 17) return @"Letter R";
if (section == 18) return @"Letter S";
if (section == 19) return @"Letter T";
if (section == 20) return @"Letter U";
if (section == 21) return @"Letter V";
if (section == 22) return @"Letter W";
if (section == 23) return @"Letter X";
if (section == 24) return @"Letter Y";
if (section == 25) return @"Letter Z";
return @"Other";
}
- (void)viewDidUnload {
}
- (void)dealloc {
[super dealloc];
}
@end
I added a table to my app, and with that table, I added a scroll bar, just like the contancts in the iPhone. The scroll is working with the letters, but i have a problem with it, under letter A, i get letter A names, but in letter B I'm getting the same names of letter A.
The same words in the .plist of the array of list A is repeating in all the rest of the lists.
What can I do??
Note: add your own .plist file to see what outcome it gives.
#import "SimpleTableViewController.h"
@implementation SimpleTableViewController
@synthesize exercises;
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
if(searching)
return nil;
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
[tempArray addObject:@"A"];
[tempArray addObject:@"B"];
[tempArray addObject:@"C"];
[tempArray addObject:@"D"];
[tempArray addObject:@"E"];
[tempArray addObject:@"F"];
[tempArray addObject:@"G"];
[tempArray addObject:@"H"];
[tempArray addObject:@"I"];
[tempArray addObject:@"J"];
[tempArray addObject:@"K"];
[tempArray addObject:@"L"];
[tempArray addObject:@"M"];
[tempArray addObject:@"N"];
[tempArray addObject:@"O"];
[tempArray addObject:@"P"];
[tempArray addObject:@"Q"];
[tempArray addObject:@"R"];
[tempArray addObject:@"S"];
[tempArray addObject:@"T"];
[tempArray addObject:@"U"];
[tempArray addObject:@"V"];
[tempArray addObject:@"W"];
[tempArray addObject:@"X"];
[tempArray addObject:@"Y"];
[tempArray addObject:@"Z"];
return tempArray;
}
- (void)viewDidLoad {
NSString *myFile = [[NSBundle mainBundle] pathForResource:@"A" ofType:@"plist"];
exercises = [[NSArray alloc]initWithContentsOfFile:myFile];
[super viewDidLoad];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section ==0) return 5;
if (section ==1) return 2;
if (section ==2) return 5;
if (section ==3) return 5;
if (section ==4) return 6;
if (section ==5) return 5;
if (section ==6) return 5;
if (section ==7) return 5;
if (section ==8) return 5;
if (section ==9) return 5;
if (section ==10) return 5;
if (section ==11) return 5;
if (section ==12) return 5;
if (section ==13) return 5;
if (section ==14) return 5;
if (section ==15) return 5;
if (section ==16) return 5;
if (section ==17) return 5;
if (section ==18) return 5;
if (section ==19) return 5;
if (section ==20) return 5;
if (section ==21) return 5;
if (section ==22) return 5;
if (section ==23) return 5;
if (section ==24) return 5;
if (section ==25) return 5;
return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil)
{
cell=[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle/*Default*/// change default to subtitle
reuseIdentifier:@"cell"];
}
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
//cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
cell.textLabel.text=[exercises objectAtIndex:indexPath.row];
cell.detailTextLabel.text=@"Click me...";
return cell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 26;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:
(NSInteger)section
{
if (section == 0) return @"Letter A";
if (section == 1) return @"Letter B";
if (section == 2) return @"Letter C";
if (section == 3) return @"Letter D";
if (section == 4) return @"Letter E";
if (section == 5) return @"Letter F";
if (section == 6) return @"Letter G";
if (section == 7) return @"Letter H";
if (section == 8) return @"Letter I";
if (section == 9) return @"Letter J";
if (section == 10) return @"Letter K";
if (section == 11) return @"Letter L";
if (section == 12) return @"Letter M";
if (section == 13) return @"Letter N";
if (section == 14) return @"Letter O";
if (section == 15) return @"Letter P";
if (section == 16) return @"Letter Q";
if (section == 17) return @"Letter R";
if (section == 18) return @"Letter S";
if (section == 19) return @"Letter T";
if (section == 20) return @"Letter U";
if (section == 21) return @"Letter V";
if (section == 22) return @"Letter W";
if (section == 23) return @"Letter X";
if (section == 24) return @"Letter Y";
if (section == 25) return @"Letter Z";
return @"Other";
}
- (void)viewDidUnload {
}
- (void)dealloc {
[super dealloc];
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您忽略了 cellForRowAtIndexPath 中的部分编号,因此,如果当 tableView 请求第 1 部分第 3 行(B 部分中的第四个条目)时,那么您将返回“A”部分中练习数组中的第四个条目。您需要重组“练习”以允许您按部分(第一个字母)索引,然后按行索引,或者计算练习中的适当条目。
您应该阅读 Apple 关于 TableViews 的文档。有关创建数组数组的帮助,请参阅“创建和配置表视图”部分,称为“填充索引列表”。 Apple 的 TableView 套件中有一个示例。
如果您确实不想按照建议将 plist 重组为数组数组,那么您可以按如下方式强力计算练习矩阵中的相应行...
而不是
尝试:
You're ignoring the section number in cellForRowAtIndexPath, so if when the tableView asks for section 1 row 3 (the fourth entry in the B section), then you're returning the fourth entry in your exercises array in the "A" section. You'll either need to restructure "exercises" to allow you to index by section (first letter), then by row, OR calculate the appropriate entry in exercises.
You should read Apple's documentation on TableViews. See "Creating and Configuring a Table View" section called "Populating an Indexed List" for help in creating an array of arrays. There's an example in Apple's TableView suite.
If you really don't want to restructure your plist into an array of arrays as suggested, then you can brute-force calculate the appropriate row in your exercises matrix as follows...
Instead of
Try:
您绝对应该更改数据的结构。我建议您使用嵌套数组。
您将创建一个根数组。在这个数组中,您放置了 26 个数组,其中每个数组保存一个部分的内容。
而不是编写
if (section ==0) return 5;
你可以写如果(部分==1)返回2;
if (section ==2) 返回 5;
if (section ==3) 返回 5;
if (section ==4) 返回 6; ...
return [[exercises objectAtIndex:section] count];
一行而不是 26。很酷,不是吗?
在 cellForRowAtIndexPath 中,您可以编写如下内容:
you should definitely change the structure of your data. I would recommend you to use nested arrays.
You would create one rootarray. And in this array you put 26 arrays where each array holds the content for one section.
and instead of writing
if (section ==0) return 5;
you could writeif (section ==1) return 2;
if (section ==2) return 5;
if (section ==3) return 5;
if (section ==4) return 6; ...
return [[exercises objectAtIndex:section] count];
One line instead of 26. Cool, isn't it?
And in cellForRowAtIndexPath you would write something like this: