我对任何类型的设备都非常陌生,并且处于陡峭的学习曲线上,所以如果这没有太多意义或问题中的代码很糟糕,请原谅我 - 我们都必须从某个地方开始,相信我,我读过读过读过!
我正在从一个字典数组的 plist 创建一个表 - 我需要这种格式的表格,因为稍后我希望能够更改一些值并写回相关的 plist 'key'。我希望有人能够看到这个并给我一些关于如何...
- 使表格以分组样式以 A 到 Z 的方式对 plist 中的键/值“名称”下的数据进行排序 - 这似乎没问题,但是,它会根据plist 中的数组,而某些字典应组合在同一部分下(见下文)
- 根据 plist 中的项目将表格分成几个部分,即。如果我有 7 个项目,跨越字母表,那么我想要 7 个部分。
- 在右侧有一个索引,只有相关数量的条目 - 如果我在“Q”下没有任何数据,那么我不希望“Q”显示在索引中!
显然,我距离整理所有这些还有很长的路要走 - 代码本身现在有点像“狗晚餐”,因为我一直在尝试很多不同的事情但没有取得多大成功,所以如果你看到一些你不喜欢的东西请告诉我!
我一直在尝试阅读所有相关部分,例如 UILocalizedIndexedCollation 和 sortedArrayUsingDescriptors,但我想我的大脑无法满足它......
任何和所有的建议(除了'放弃它你不够聪明'因为我从不放弃我开始的任何事情!)将不胜感激!
(在开始时合成了许多未使用的变量 - 为了简化我在此处发布的内容,我删除了相关代码,代码编译没有问题,并且工作给了我以下结果:索引 27 个字母的表在右侧只有 AJ 起作用(这与表中生成的部分数量相关 - 只有 AJ 部分。单元格的内容正是我想要的。)
#import "RootViewController.h"
#import "View2Controller.h"
#import "tableviewsAppDelegate.h"
#import "SecondViewController.h"
#import "HardwareRootViewController.h"
#import "HardwareSecondViewController.h"
#import "SoftwareRootViewController.h"
@implementation SoftwareRootViewController
@synthesize dataList2;
@synthesize names;
@synthesize keys;
@synthesize tempImageType;
@synthesize tempImageName;
@synthesize finalImageName;
@synthesize tempSubtitle;
@synthesize finalSubtitleName;
@synthesize tempSubtitleType;
@synthesize finalSubtitleText;
@synthesize sortedArray;
@synthesize cellName;
@synthesize rowName;
//Creates grouped tableview//
- (id)initWithStyle:(UITableViewStyle)style {
if (self = [super initWithStyle:UITableViewStyleGrouped]) {
}
return self;
}
- (void)viewDidLoad {
//loads in backgroundimage and creates page title//
NSString *backgroundPath = [[NSBundle mainBundle] pathForResource:@"background1" ofType:@"png"];
UIImage *backgroundImage = [UIImage imageWithContentsOfFile:backgroundPath];
UIColor *backgroundColor = [[UIColor alloc] initWithPatternImage:backgroundImage];
self.tableView.backgroundColor = backgroundColor;
[backgroundColor release];
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:.5 green:.4 blue:.3 alpha:5];
self.title = @"Software";
[super viewDidLoad];
//Defines path for DATA For ARRAY//
NSString *path = [[NSBundle mainBundle] pathForResource:@"DataDetail3" ofType:@"plist"];
//initialises the contents of the ARRAY with the PLIST//
NSMutableArray* nameArray = [[NSMutableArray alloc]
initWithContentsOfFile:path];
//Sorts the items in the list alphabetically//
NSSortDescriptor *nameSorter = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];
[nameArray sortUsingDescriptors:[NSArray arrayWithObject:nameSorter]];
[nameSorter release];
self.dataList2 = nameArray;
[nameArray release];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release anything that can be recreated in viewDidLoad or on demand.
// e.g. self.myOutlet = nil;
}
#pragma mark Table view methods
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SectionsTableIdentifier ];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier: SectionsTableIdentifier ] autorelease];
}
// Configure the cell.
cell.indentationLevel = 1;
cell.textLabel.text = [[self.dataList2 objectAtIndex:indexPath.row]
objectForKey:@"name"];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
//Detremines the cell color according to the value in 'owned' in the plist//
NSString *textColor = [[self.dataList2 objectAtIndex:indexPath.row]
objectForKey:@"owned"];
if ([textColor isEqualToString: @"greenColor"]) {
[cell setBackgroundColor:[UIColor colorWithRed:0.1 green:0.7 blue:0.1 alpha:1]];
}
if ([textColor isEqualToString: @"blackColor"]) {
[cell setBackgroundColor:[UIColor whiteColor]];
}
return cell;
}
// Code For Loading of The View2Controller//
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [[self.dataList2 objectAtIndex:indexPath.row]
objectForKey:@"name"];
NSString *rowTitle = CellIdentifier;
NSLog(@"rowTitle = %@", rowTitle);
[tableView deselectRowAtIndexPath:indexPath animated:YES];
SecondViewController *second = [[SecondViewController alloc] init];
[second setCategory: rowTitle];
[self.navigationController pushViewController:second animated:YES];
[second release];
}
- (void)dealloc {
[dataList2 release];
[super dealloc];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [dataList2 count];
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [dataList2 count]; ///Tells the table that it only needs the amount of cells listed in the DATALIST1 ARRAY//
}//
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)help
{
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:help];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
@end
任何建议都将不胜感激 - 目前我已经为此工作了大约一个星期,但没有成功,几乎要放弃了,我真的不想这样做!
如果最坏的情况发生在最坏的情况下并且我无法完成这项工作,是否有人愿意为我编写功能,当然需要支付少量费用!
干杯,希望...
鲢鱼