2010-08-02 50 views
1

我對編程任何類型的設備都很新穎,而且學習曲線陡峭,所以請原諒我,如果這樣做沒有太大意義,或者問題中的代碼很糟糕 - 我們都要開始某個地方,並且相信我,我已經閱讀並閱讀!iPhone - 按照字母順序創建桌面視圖

我從plist中創建一個表,這是一個字典數組 - 我需要這種格式,因爲後來我希望能夠更改一些值並將其寫回相關的plist'鍵'。 我希望有人能夠看到這一點,並給我一些指點至於如何...

  1. 從plist中,務必鍵/值「名」排序的數據表時尚A到Z的分組風格 - 這似乎是好的,但是,它會根據plist數組中字典的數量創建部分的數量,而某些字典應該在同一部分下組合在一起(請參閱以下)
  2. 根據plist中的項目將表格分成不同的部分。如果我有7個項目,跨越字母表,然後我想要7個部分。
  3. 在右側有一個索引,只有相關數量的條目 - 如果我沒有'Q'下的任何數據,那麼我不想'Q'顯示在索引中!

很明顯,我很長的一段路要將所有這些排除出去 - 代碼本身有點像'狗晚餐',因爲我一直在嘗試這麼多不同的事情,但沒有太多成功,所以如果你看到你不喜歡的東西請讓我知道!

我一直在試圖對所有相關的部分,如UILocalizedIndexedCollat​​ion和sortedArrayUsingDescriptors讀了,但我想我的大腦只是心不是到它...

任何及所有意見(除了「放棄你」對於這個'不夠光明',因爲我從不放棄任何我開始!')將不勝感激!

(有很多未使用的變量在開始時合成 - 爲了簡化我在這裏發佈的內容,代碼編譯沒有問題,並且給出了以下結果: a表格中有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 

任何意見,將不勝感激 - 目前我一直在這一個工作了一個星期沒有成功,接近放棄,我真的不想要做!

如果最糟糕的事情發生了,我不能完成這個任務,我會願意爲我寫這些功能,當然是一小筆費用!

乾杯,和繼承人希望...

Chubs

回答

1

使用UILocalizedIndexCollat​​ion =]