views:

366

answers:

2

Hi, everyone,

I want to ask a question about the UITableView of the objective C. I am writing a program and I would like to create the UI programmatically. However, I don't know how to display the table programmatically. I already have a NSMutableArray to store the displayed data. And I create a object UITableView *tableData;, what should I do for the next step? Thank you very much.

+3  A: 

Something like this should do the trick (assuming this is running from within your view controller and you have a property set up for the table view):

tableView = [[[UITableView alloc] initWithFrame:CGRectMake(...) style:UITableViewStylePlain] autorelease];
tableView.dataSource = self;
tableView.delegate = self;

[self.view addSubview:tableView];

Where you replace CGRectMake(...) with whatever position / size you'd like.

Chris Doble
@Chris Doble, thank you for your reply. But I want to ask, where should I put the NSMutableArray? thank you.
Questions
You would store the array in your view controller and use it in the UITableViewDataSource methods. For example, you would return the length of the array in tableView:numberOfRowsInSection:.
Chris Doble
@Chris Doble, thank you for your reply. Because the UI contains others another element, I add the UITableView as a part of them. And I don't know how to call the UITableDataSource as the class is not a table view class when I create it. Would you mind to tell me how to import or add the data in the array? thank you.
Questions
To supply data to the `UITableView`, your view controller must conform to the [`UITableViewDataSource` protocol](http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableViewDatasource_Protocol/Reference/Reference.html). It is from these methods that you return your data.I suggest you download and have a look through Apple's [TableViewSuite](http://developer.apple.com/iphone/library/samplecode/TableViewSuite/Introduction/Intro.html) sample to get an idea of how to use table views.
Chris Doble
+2  A: 

after writing above code you have to implement delegate method of UITableView.

these are delegate method of UITableView load

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; }

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { retrun [your array count]; }

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

}

// Configure the cell...
cell.textLabel.text = [yourarray objectAtIndex:indexPath.row];

return cell;

}

priyanka
@priyanka, thank you for your reply. But I am not using the UIViewTable class, I just using the simple class. Would you mind to tell me how to add the array to the table or table cell? Thank you.
Questions
@MarkSiu there is no need to implement UITableView Class just write code to add UITableView in your viewdidload function and above three function its show table in your view
priyanka
@priyanka, thank you for your reply. I have added the above functions in the .m class. However, I found that those functions did not be called by the UITableView. When do the functions be called? Thank you.
Questions
@MarkSiu you add this code in viewdidload then that function are automatic calltableView = [[[UITableView alloc] initWithFrame:CGRectMake(...) style:UITableViewStylePlain] autorelease];tableView.dataSource = self;// this lines are call that functionstableView.delegate = self;// this lines are call that functions[self.view addSubview:tableView];
priyanka