How to dynamically update your UITableView when a button is pressed? - iphone

I'm stuck with a seemingly very simple app that I'm trying to develop
Basically, I have a UITable, and two buttons: red/blue
When a button is pressed, a row with corresponding title of that button is append to the table
I'm overwhelmed by how complicated UITableView has to be implemented (datasource, delegate, resuable identifier, etc)
Can anyone help me out with this, preferably show me detailed codes
For my Buttons, I have something like this
- (IBAction)buttonPressed:(UIButton *)sender{
NSString *item = sender.currentTitle;
[self.cellArray addObject:item];
[self.myTable reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.cellArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"The Table Cell";
self.myTableCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (self.myTableCell == nil) {
self.myTableCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
self.myTableCell.textLabel.text = [self.cellArray objectAtIndex:indexPath.row];
return self.myTableCell;
}

Since you are learning this..i will post a simple solution.
Make add a member variable NSMutableArray *cellArrays;
initialize it in your viewDidLoad
in buttonPressed check;
if([#"red" isEqualToString:[YourButton titleForState:UIControlStateNormal]])
{
[self.cellArrays addObject:#"red"];
}
else
{
[self.cellArrays addObject:#"blue"];
}
[self.YOURTABLEVIEW reloadData];
Now in your table view datasource method
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.cellArrays.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//in between your code;
YOURCELL.textlabel.text = [self.cellArrays objectAtIndex : indexpath.row];
}
Try this ..

I'd recommend subclassing UITableViewController, then override the dataSource and delegate methods and you should be good to go. Going straight to UITableView is more complicated without any obvious benefits.
You may not be using Core Data, but I still recommend this lecture because it hooks up a TableViewController and works great - all code included. Check it out:
http://www.stanford.edu/class/cs193p/cgi-bin/drupal/node/289
The lectures in iTunes U explain everything further.
Enjoy,
Damien

I wanted to add that "One more thing, the compiler shows a warning says no reusable identifier. what exactly is that?" above...
His answer is correct, but to get rid of that compiler warning...
The tableviewcell of your tableview has to have an identifier. In IOS 5, in your storyboard, highlight the TableViewCell in your TableView, and enter a value in the Identifier. This must match the value in your code of the cell that you are creating.

Related

Tableview inside Flipview using Utility template

I'm new to iOS dev, so I do apologise for asking potentially dumb question.
What I want to do is something very similar to the default Weather app; where the app has a info button, it flips to another view which has a table along with a Done button to go back to the app.
I'm using the 'Utility Application' template which does most of this for me :)
However, I am now struggling to add a tableview into the flipview. Am I on the right path? I'm using storyboard at the moment - beginning to realise this is most likely a limitation on the GUI (after all a GUI can only go so far). If so, is this possible programatically and how would I go about applying it on a default 'Utility Application' template.
I'm using Xcode 4.2.
Any help would be appreciated. Thanks in advance :)
First of all, you'll want to drag and drop a UITableView onto your flipsideViewController in interface builder. Make sure that you like its delegate and data source to the view controller.
Then change flipsideViewController.h to create an instance variable for the array that will store the text for the cells labels and for the controller to conform to the tables delegate and data source methods.
#interface FlipsideViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
NSArray *myArrayOfItems;
}
In flipsideViewController.m alloc/init and populate your array in viewDidLoad
myArrayOfItems = [[NSArray alloc] initWithObjects:#"firstItem",#"secondItem",#"thirdItem",#"fourthItem", nil];
And finally, copy and paste the following and you should have a working table!
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [myArrayOfItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [myArrayOfItems objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Selected cell index:%i",indexPath.row);
}

Linking UITableView with a NSMutableArray

I've been trying to fill a UITableView with data from a NSMutableArray. I have a View-Based application project and so far I've tried every tutorial I found but so far, it just doesn't work. I linked the dataSource and delegate to the File's Owner, I added the procotols UITableViewDelegate and UITableViewDataSource to my UIViewController, I create an array containing 3 strings just to try and debug, everytime I load the app, the table stays empty.
I'm not sure what could be my error, or where I should look to find the problem, if you guys got any idea that would be nice.
Thank you
-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection: (NSInteger)section
{
return [yourArray count];
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [yourArray objectAtIndex:row];
return cell;
}
That should populate the table from an array. I've used it several times in my app.
I guess you did forget to call -reloadData on the table view.
Make sure the dataSource and delegate are properly connected to the view controller, also make sure your UITableView is property connected with the outlet if you are using storyboards or a xib file.
If nothing works I recommend you to use XLData that makes UITableView data loading much more simple to implement.

How to show text in a UITableView

I have a table and I want to show there some text; but I can't, I don't know why. My app is a view-based app, and I don't want to change it for a Table View Controller. Here's the code:
.h
{
NSArray *array;
IBOutlet UITableView *table;
}
.m
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.textLabel.text = [array objectAtIndex:indexPath.row];
// Configure the cell
return cell;
}
Please help me because I'm trying to solve this and I don't know what's wrong, why the Table doesn't show the array!
Thank you!
You almost certainly haven't connected your datasource and delegates properly in IB.
Your UIViewController needs to implement UITableViewDelegate and UITableViewDatasource, and you need to then connect it to your UITableView (table) as such in Interface Builder.
For details, read through Apple's documentation here.
On your .H file are you using this interfaces <UITableViewDelegate, UITableViewDataSource>?
The answer is that you've never ever created single cell. The call you make dequeueReusableCellWithIndentifier only recycles cells you've already created. Add this code after the dequeueResusable... line:
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

Is there a way in xcode 4 to list all delegate functions and automatically add the code snippet to a file?

This seems like an obvious feature that should be there but i can't find it.
For example if my class is a uitableviewdelegate whats the quickest way for me to see all the available delegate methods and add the ones im interested in to my implementation file?
You can use the tip in the first link that Simon posted above to get the method signatures for a delegate. After that, adding the code is up to you.
What I like to do is to choose the delegate methods that I most often use, and add them to a code snippet.
For example, for UITableViewDatasource I have a snippet called "UITableView Datasource Required Methods" containing:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"myCellName";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease];
}
//customize cell before showing it
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return <#numRow#> ;
}
Then I drag that snippet into my code whenever I'm creating a table delegate.

Adding Data/Rows to UITableView (Multiple Tables on one view)

I've searched around and I can't seem to figure out how to do this. It doesn't help that I don't really know what I am totally doing yet, but I hope this will help.
I am creating an iPad application. In short it is a complex stopwatch that will take splits (for running) on one view.
I have a master clock, and 5 buttons for separate splits. All that works. But, I want to record these splits and I thought it would be great to do it in a table that can be scrolled through.
I have 5 UITableViews on the one view. I found some stuff online for a "datasource protocol" and got everything working great for just one table. Things went to crap when I tried to make it work separately for each table. Also, it seemed like a ton of code for a simple task.
I have 5 mutable arrays already present. I really don't know how to go about this and any help would be great!
Also, if possible, i need to clear the tables with the press of a button...seems simple, but I truly don't know.
Thanks!
You need to set UITableViewDelegate and UITableViewDataSource for all your tables to a class that will implement the following methods:
For UITableViewDataSource:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
and
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
As these methods are passed the calling tableview, it should be easy for you to return the correct data for the tableview in question (which you are already storing in an NSMutableArray). You still need to cater for displaying different content for your arrays, but I trust you will manage to do so. NSIndexPath basically tells you which part of your array should be displayed. Assuming, for now, that you are working in an ungrouped table, you would simple create a new cell and fill it with the contents of your array, which is determined by the indexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyNiceIdentifier";
cell = [aTableView dequeueReusableCellWithIdentifier:NavigationCellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.tag = 500;
}
cell.textLabel = [myArray objectAtIndex:indexPath.row];
}
In the other data source method, you simple return the count for that array:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(tableView == myFirstTableView) {
return [myFirstArray count];
}
}
The method of UITableViewDelegate you will likely use most often is this:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
which you use to return the desired height for a cell at a given indexPath.
Setting dataSource and delegate is as simply as doing:
myTableView.delegate = ...
myTableView.dataSource = ...
See this documentation:http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UITableViewDelegate
Please also refer to this documentation:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html