2011-06-22 2 views
8

Tôi đã lập trình một UITableView và mỗi ô đẩy một chế độ xem mới, Tất cả những gì tôi muốn làm là thêm hai phần mới một nam và một nữ, giọng nói đầu tiên và thứ hai cần nằm trong phần nam và phần thứ ba giọng nói cần phải ở trong phần nữ.iPhone UITableView Phần

#import "FirstLevelViewController.h" 
#import "SecondLevelViewController.h" 
#import "DisclosureDetailController.h" 
#import "SecondVoiceController.h" 
#import "ThirdVoiceController.h" 



@implementation FirstLevelViewController 
@synthesize controllers; 

-(void)viewDidLoad { 
    self.title = @"Voices"; 

    NSMutableArray *male = [[NSMutableArray alloc] init]; 


    DisclosureDetailController *th = [DisclosureDetailController alloc]; 
    th.title = @"First Voice"; 
    [male addObject:th]; 
    [th release]; 

    SecondVoiceController *array2 = [SecondVoiceController alloc]; 
    array2.title = @"Second Voice"; 
    [male addObject:array2]; 
    [array2 release]; 

    ThirdVoiceController *array3 = [ThirdVoiceController alloc]; 
    array3.title = @"Third Voice"; 
    [male addObject:array3]; 
    [array3 release]; 




    self.controllers = male; 
    [male release]; 
    [super viewDidLoad]; 
} 

-(void)viewDidUnload { 

    self.controllers = nil; 
    [super viewDidUnload]; 
} 

-(void)dealloc { 

    [controllers release]; 
    [super dealloc]; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [self.controllers count]; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *FirstLevelCell= @"FirstLevelCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstLevelCell]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstLevelCell] autorelease]; 
    } 
    NSUInteger row = [indexPath row]; 
    SecondLevelViewController *controller = [controllers objectAtIndex:row]; 
    cell.textLabel.text = controller.title; 
    cell.imageView.image = controller.rowImage; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    return cell; 
} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSUInteger row = [indexPath row]; 

    SecondLevelViewController *nextViewController = [self.controllers 
                objectAtIndex:row]; 

    [self.navigationController pushViewController:nextViewController animated:YES]; 
} 

Tất cả điều tôi muốn làm là thêm hai phần mới một nam và một nữ, giọng nói đầu tiên và thứ hai cần nằm trong phần nam và giọng nói thứ ba cần nằm trong phần nữ. Xin vui lòng giúp đỡ bị mắc kẹt về điều này trong một thời gian!

+0

vui lòng sử dụng tiêu đề mô tả hơn – vikingosegundo

Trả lời

25

Bảng delegate có phương thức được gọi là numberOfSectionsInTableView. Trả về số phần mà bạn muốn tạo.

Sau đó, trong cellForRowAtIndexPath hãy sử dụng thuộc tính khác indexPath khác [indexPath section] để phân tách các hàng dựa trên các phần.

Một ví dụ

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 2; //one male and other female 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    switch(section){ 
     case 0: 
     return [male count]; 
     break; 
     case 1: 
     return [female count]; 
     break; 
    } 
} 


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *FirstLevelCell= @"FirstLevelCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstLevelCell]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstLevelCell] autorelease]; 
    } 
    SecondLevelViewController *controller; 
    switch([indexPath section]){ 
     case 0: 
     controller = [male objectAtIndex: [indexPath row] ]; 
     break; 
     case 1: 
     controller = [female objectAtIndex: [indexPath row] ]; 
     break; 
    } 
    cell.textLabel.text = controller.title; 
    cell.imageView.image = controller.rowImage; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    return cell; 
} 
+0

xin cảm ơn vì phản hồi nhưng vui lòng xem chi tiết hơn một số mã. Cảm ơn, Sam Houghton. – Sam

+2

táo đi vào chi tiết: http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/AboutTableViewsiPhone/AboutTableViewsiPhone.html – vikingosegundo

+0

@sam câu trả lời được cập nhật để bao gồm ví dụ –

0

bí quyết này?

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // Return the number of sections. 
    return 2; 
} 



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    switch (section) { 
     case 0: 
      return # of rows in section; 
      break; 
     case 1: 
      return # of rows in section; 
break; 
    } 
    return 0; 
} 
+0

Cảm ơn bạn đã trả lời nhưng điều đó không có tác dụng, chỉ cần đặt giọng nói đầu tiên và thứ hai trong bảng và giọng nói thứ ba đã trôi qua, và vẫn không có phần nào, cảm ơn Sam Houghton – Sam