Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

ios - Adding cells programmatically to UITableView

I've just recently started programming for the iPhone and I'm making an application that connects to a database and gets a set of row names and displays them. When selected, the rows background colour change ie you can make multiple selections and they will all be different colours. So I'm getting the XML back from the server no problem and I have created a UITableView to display the cells in. However, I have no idea how to add the cells into the table. I had a look at insertRowsAtIndexPaths but I'm not sure how to use it? As I understand, insertRowsAtIndexPaths takes two parameters:

An NSArray that contains what row the cell is supposed to be in and in what section. The problem with this is that my app will have a dynamic number of rows. How would I go about creating the NSArray if I don't know how many rows I will have? Can I use NSMutableArray?

The second parameter it takes is an animation - that's pretty straightforward.

Another issue I am having is where do I actually create the cells? How do you pass the cells to the tableview?

I've tried reading the documentation but it just doesn't seem very clear! Here is the code I have at the moment inside the loadview method of the view controller:

 //Before this I get the XML from the server so I am ready to populate
 //cells and add them to the table view
 NSArray *cells = [NSArray arrayWithObjects:
                   [NSIndexPath indexPathForRow:0 inSection:0],
                   [NSIndexPath indexPathForRow:1 inSection:0],
                   [NSIndexPath indexPathForRow:2 inSection:0],
                   [NSIndexPath indexPathForRow:3 inSection:0],
                   [NSIndexPath indexPathForRow:4 inSection:0],
                   [NSIndexPath indexPathForRow:5 inSection:0],
                   [NSIndexPath indexPathForRow:6 inSection:0],
                   [NSIndexPath indexPathForRow:7 inSection:0],
                   [NSIndexPath indexPathForRow:8 inSection:0],
                   [NSIndexPath indexPathForRow:9 inSection:0],
                   [NSIndexPath indexPathForRow:10 inSection:0],
                   [NSIndexPath indexPathForRow:11 inSection:0],
                   [NSIndexPath indexPathForRow:12 inSection:0],
                   nil];
[eventTypesTable beginUpdates];
[eventTypesTable insertRowsAtIndexPaths:cells withRowAnimation:UITableViewRowAnimationNone];
[eventTypesTable endUpdates];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you're approaching this from the wrong direction. UITableViews don't work as you are expecting. insertRowsAtIndexPaths is for inserting new rows into a table, rather than for populating it in the first instance.

UITableViews work by calling a number of delegate methods that allow you to present your data to the table view as you need to. The framework then takes care of the heavy lifting to populate cells, handle scrolling and touch events, etc.

I would recommend that you start by reading a tutorial such as this one: http://www.iosdevnotes.com/2011/10/uitableview-tutorial/ , which looks fairly thorough to me. It explains how to set your datasource for the table and how you can configure the way your data is presented by the UITableView.

Good luck!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...