views:

82

answers:

1

Hi, everyone,

I want to ask a question about the iPhone application. I am writing a program with the UINavigationController, and I can click the UITableView cell and go to the detail page. However, I don't know how to create the following table view. (I use the didSelectRowAtIndexPath to go to another page) How can I create this kind of table? As same as the UITableViewController?

alt text

link: http://www.freeimagehosting.net/image.php?02730817e4.png

// ---- Update -----

The detailViewController.h

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {

    UIView *view;
    UITableView *tableView;
}

@property (nonatomic, retain) UIView *view;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, assign) id<UITableViewDataSource> dataSource;
@property (nonatomic, assign) id<UINavigationControllerDelegate> delegate;

@end

DetailViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];


    CGRect cgRct = CGRectMake(0.0, 0.0, 480, 320);
    view = [[UIView alloc] initWithFrame:cgRct]; 
    view.autoresizesSubviews = YES;           
    self.view = view;                 

    tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 480.0f, 400.0f) style:UITableViewStyleGrouped] autorelease];
    [view addSubview: tableView]; 

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 3;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [dataArray count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    NSString *title = nil;

    switch (section) {
        case 0:
            title = @"First Name";
            break;
        case 1:
            title = @"Middle Name";
            break;
        case 2:
            title = @"Last Name";
            break;
        default:
            break;
    }

    return title;

}

How to add the data from the clicked cell?

+2  A: 

It is standard UITableView with its style set to UITableViewStyleGrouped. "Date", "Genre" and "Main characters" are section headers - set them using tableView:titleForHeaderInSection: method in data source.

Vladimir
@Vladimir, thank you for your reply. As I am the green of the iPhone application. Would you mind to give more explain to me? and how to set the tableView:titleForHeaderInSection:? Thank you.
Questions
You should implement tableView:titleForHeaderInSection: method in your table's data source (the same way you implement cellForRowAtIndexPath: method for example). Also that table has several sections - you need to implement numberOfSectionsInTableView: method and return appropriate value from it (3 in your case)
Vladimir
@Vladimir, thank you for your reply. I modified the code accordingly. However, I do have a problem which is the data source. I use an array to display in the rootViewController, how do I know which of the element is selected in the table and how to display in the table format like the above. Thank you. (please see the above post, I have updated.)
Questions
It depends on what your "Details" data is? As a most general example you can use NSDictionary containing arrays - one array for section and use array key for section header title and array itself for cell's data for corresponding section.
Vladimir
@Vladimir, thank you for your reply. If I store all the data in the NSMutableArray and the rootViewController.m. Is it possible to convent the NSMutableArray to the NSDictionary? and where should I do it?
Questions
You store all the data in array, but in details controller you show only one selected item, don't you? You can define a property in details controller to store that item so controller can display it (and not be aware of the whole data - as it possibly does not need to...). Then everything depends on what your item actually is.
Vladimir