23

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.

willcodejavaforfood
  • 43,223
  • 17
  • 81
  • 111
Questions
  • 20,055
  • 29
  • 72
  • 101

4 Answers4

43

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.

  • thank you for your reply. But I want to ask, where should I put the NSMutableArray? thank you. – Questions Jul 26 '10 at 09:31
  • 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:. –  Jul 26 '10 at 09:46
  • 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 Jul 26 '10 at 09:59
  • 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. –  Jul 26 '10 at 10:14
38

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 {
    return [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;

}
Alec Gorge
  • 17,110
  • 10
  • 59
  • 71
priyanka
  • 2,076
  • 1
  • 16
  • 20
  • 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 Jul 26 '10 at 10:03
  • @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 Jul 26 '10 at 11:42
  • 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 Jul 27 '10 at 01:02
  • 1
    @MarkSiu you add this code in viewdidload then that function are automatic call tableView = [[[UITableView alloc] initWithFrame:CGRectMake(...) style:UITableViewStylePlain] autorelease]; tableView.dataSource = self;// this lines are call that functions tableView.delegate = self;// this lines are call that functions [self.view addSubview:tableView]; – priyanka Jul 27 '10 at 04:43
  • 1
    @priyanka I think time may have destroyed part of your context, because there isn't any code above yours. – RonLugge Aug 24 '12 at 20:23
  • 1
    BTW if you're using the ios6 introduced `dequeueReusableCellWithIdentifier:forIndexPath:` (diff. method) like I am, you also need this call in advance: [tableView registerClass:[UITableViewCell class]forCellReuseIdentifier:CellIdentifier]; – huggie Dec 07 '12 at 11:01
21
  • Create a new class that inherits from UIViewController.
  • Make it conform to the UITableViewDataSource protocol.
  • Declare your table view.

Your header file should be like the following:

@interface MyViewController : UIViewController <UITableViewDataSource> {    

}

@property (nonatomic, retain) UITableView *tableView;

@end

In the viewLoad method of your class:

  • Create a table view using initWithFrame. Use dimensions 320x460 for full height. Remove 44 from height if you have a navigation bar and 49 if you have a tab bar.
  • Create a new view.
  • Add the table view to the new view.
  • Set the controller view to the new view.
  • Set the table view data source to your instance (self).
  • Implement the two required data source methods: tableView:cellForRowAtIndexPath and tableView:numberOfRowsInSection

Your implementation file should be like the following:

#import "MyViewController.h"

@implementation MyViewController

@synthesize tableView=_tableView;

- (void)dealloc
{
    [_tableView release];

    [super dealloc];
}

#pragma mark - View lifecycle

- (void)loadView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 460.0) style:UITableViewStylePlain];
    self.tableView = tableView;
    [tableView release];    

    UIView *view = [[UIView alloc] init];
    [view addSubview:self.tableView];
    self.view = view;
    [view release];

    self.tableView.dataSource = self;
}

- (void)viewDidUnload {
    self.tableView = nil;

    [super viewDidUnload];
}

#pragma mark - Table view data source

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyCellIdentifier = @"MyCellIdentifier";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyCellIdentifier];

    if(cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyCellIdentifier] autorelease];
    }

    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   return 5;
}

@end
3

Maybe its also helpful for you all who new in there.

- (void)viewDidLoad
{
    [super viewDidLoad];
    // init table view
    tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];

    //or, you may do that 
    //tableView = [[UITableView alloc] init];
    //tableView.frame = CGRectMake:(5 , 5 , 320 , 300);

    // must set delegate & dataSource, otherwise the the table will be empty and not responsive
    tableView.delegate = self;
    tableView.dataSource = self;

    tableView.backgroundColor = [UIColor cyanColor];

    // add to canvas
    [self.view addSubview:tableView];
}

#pragma mark - UITableViewDataSource
// number of section(s), now I assume there is only 1 section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView
{
    return 1;
}

// number of row in the section, I assume there is only 1 row
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

// the cell will be returned to the tableView
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"HistoryCell";

    // Similar to UITableViewCell, but 
    JSCustomCell *cell = (JSCustomCell *)[theTableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[JSCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    // Just want to test, so I hardcode the data
    cell.descriptionLabel.text = @"Testing";

    return cell;
}

@end
Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
Sumit singh
  • 2,398
  • 1
  • 15
  • 30
  • 2
    for new folks (me), to copy/paste into a generic app, change 'JSCustomCell' to 'UITableViewCell', change 'cell.descriptionLabel.text' to 'cell.textLabel.text'. return count of your array items for 'numberOfRowsInSection'. reminder to declare delegate methods (i think that is what it is called) in .h file, ''. (thank you Abhay) – tmr Apr 06 '15 at 23:06