1

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 3

2

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;
}
Sign up to request clarification or add additional context in comments.

8 Comments

hey thanks for such a quick response! I tried what you said but when I built it, it instantly crashed and the log said "Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:". Do you have any ideas what may have caused this?
That could be a number of things...if you set a breakpoint just before return cell and in the debugger type po cell, what's the output?
I'm not positive I placed the second breakpoint in the right location but the app still crashed but said "stopped at breakpoint 2." I don't think I placed the second one right, sorry but where exactly is the po cell in the debugger?
The same place you see NSLog output. It's the console. (lldb) po cell or if you have gdb, (gdb) po cell
Hey very sorry for being so annoying, but I have been looking for the past ten minutes for the po cell line. Is it because I just have the log displaying at the bottom pull-up window?
|
2

Just 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

I totally agree with this..erm, comment. Table views use delegation, reusable pools, and a data source, which are all difficult concepts for starters.
hey thanks for the link to that tutorial. I checked it out and its explaining how to use the UITableView in a navigation application. The process it shows doesn't really help me out too much. Thanks though!
0

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.

2 Comments

Hey thanks for the response! I am for sure going to check this out
No problem. The whole lecture series is really good; just use with caution since it was created based around iOS 5, so things like MapKit and being required to @synthesize Properties have changed since then. But the core basics are still the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.