UITableView in a UIViewController - how do I manage? newcomers question
Finally managed to start fiddling with Xcode and Objective-C since yesterday. What I have now is
A UIViewController which has in it
- a UITextView
- a Tab bar with a view Tab buttons (not functional yet)
- a TableView
I have coded basic functionality where a keyboard pops up on the TextView, and when you enter text and hit 'done', I am able to capture the text (using IBOutlet, IBAction etc.) in a method which shows an alert with the text entered. Essentially I can now capture the开发者_JAVA百科 entered text within my code.
What I want to do now is to enter this text as a row in the TableView - and I'm sort of lost now amidst controllers and views.
Where does the TableViewController fit into all this? I already have a TableView within this view controller, so how do I interact with it? Should I "somehow" be passing control to a separate TableViewController which I should create and add to my project? Or should I add a TableViewController to this UIViewController? I'm a bit confused.
Any pointers appreciated. You don't need to get into a lot of detail even - just a list of bullets that even say 'do this/do that' will help me go chasing the concepts. Right now I don't know which way to turn.
Thanks a bunch!
Check out the UITableViewDataSource protocol. In particular you will need to implement the numberOfRowsInSection and cellForRowAtIndexPath methods.
You will pass your text into the cell's textlabel -- check out the UITableViewCell class for more info on that.
If you are trying to add the uitableview to your view controller that includes the entry box, I would create the IBOutlet for the uitableview, add the tableview to your NIB and attach the delegate to file owner and attach the table to the IBOutlet. Then you will need to add an NSMutableArray (property, synthesize, and release) to your view controller which will be used when an item is added from the text box. Lastly each time an item is added to the text box, you call [myTable reloadData]; to refresh the tableview now that something else has been added.
As William mentioned in a seperate answer, you will to implement all of the required delegate methods for the UITableView (numberOfRowsInSection and cellForRowAtIndexPath) which will all work off of your array you are building.
There are more things to take into account such as should this data persist after the app closes but this should give you a start. Hope this helps!!
精彩评论