Hey I have been looking all over the internet and this site for hours and nothing has come up which can solve my problem. I am very new to iPhone programming so I am sorry if this question seems too nooby. I am attempting to add a UITableView into my main view controller (part of a view-based application), and then populate its cells with strings from an array (which I already have set up). I have tried cutting and pasting the code from a default navigation based app, yet that doesn't work because the view-based app doesn't know how to react to the code. My question is how should I go about populating the table (which I dragged into the xib) with the array I made? ANY help is GREATLY appreciated. Thanks in advance!
3 Answers
In SomeSubclassOfUITableViewController.m
First, implement the delegate methods:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_myStrings count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
Then assign titles/subtitles/etc.
- (UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"TableCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier];
}
cell.textLabel.text = _myStrings[indexPath.row];
return cell;
}
8 Comments
return cell and in the debugger type po cell, what's the output?NSLog output. It's the console. (lldb) po cell or if you have gdb, (gdb) po cellJust wanted to mention that Apple has a great tutorial covering UITableViews and some other basics. So if you have trouble understanding the code John posted, check it out: Your Second iOS App
(I was going to add this as a comment, but I'm not awesome enough yet)
2 Comments
UITableView's make use of delegation, which can be tricky when you're first starting out coding in Objective-C and for iOS.
Paul Hagerty, a computer science professor at Stanford University, has a fantastic lecture series on programming for iOS and in Objective-C up on iTunes U here.
The 9th lecture covers the basics for UITableViews.