开发者

iPhone dev - Having problems adding text to cells of a UITableView

开发者 https://www.devze.com 2023-03-19 12:00 出处:网络
I am trying to make an app that will display a bunch of different people\'s names and address in different cells of a UITableView. First I\'m just t开发者_开发问答rying to get the hand of adding text

I am trying to make an app that will display a bunch of different people's names and address in different cells of a UITableView. First I'm just t开发者_开发问答rying to get the hand of adding text to the cells, and it's not really working. I have the following code, where myTableViewController is just a subclass of UITableViewController that loads in its view from a nib (without any drastic customizations):

    myTableViewController * tvc = [[myTableViewController alloc] initWithStyle:UITableViewStylePlain];

    NSIndexPath *path1 =[NSIndexPath indexPathForRow:0 inSection:0];
    NSIndexPath *path2 =[NSIndexPath indexPathForRow:1 inSection:0];

    [self presentModalViewController:tvc animated:YES];

    UITableViewCell * cell1 = [tvc.tableView cellForRowAtIndexPath:path1];
    UITableViewCell * cell2 = [tvc.tableView cellForRowAtIndexPath:path2];

    cell1.textLabel.text = @"test";
    cell2.textLabel.text = @"test2";

I was expecting to see a tableView animate onto the screen, followed by the first and second cells being filled with the text "test" and "test2" respectively. What I get is a blank tableview animating onto the screen. Then if I scroll down so that the top cell is hidden and scroll back up so that the top cell is again visible, the top cell will now have the text "test" in it. But the second cell down never shows any text, just the top one.

Can someone please point me in the right direction? I also would like to have the text in the tableview BEFORE it loads onto the screen, but it looks like the cells don't exist unless they are being displayed. Please help me.


Populating a table view takes the form of a data-source pattern, where the table view calls your data source object for data to put in individual cells.

Try reading through the Table View Programming Guide.


You should use the delegate and the datasource protocol methods.

0

精彩评论

暂无评论...
验证码 换一张
取 消