Push the values to new ViewController when a cell is tapped - objective-c

I have table view that populated with plist called "Htgg".
I am trying to push the value to a new view called "DetailViewController".
This is the code:
#import "Glist.h"
#import "DetailViewController.h"
#implementation Glist
#synthesize htgg,detailViewController;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"ofek", #"ofek");
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title = #"איך להוציא גימלים?";
NSString *htggFile = [[NSBundle mainBundle]pathForResource:#"Htgg" ofType:#"plist"];
htgg = [[NSDictionary alloc] initWithContentsOfFile: htggFile];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (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 [htgg 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.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
id keys = [htgg allKeys];
//Get all the keys in the first dictionary (the keys are: "good", "funny", "New - Item 3")
//This is an array, so you can do this: array[0] (in C#)
//Here we tell the tableview cell. to put in the text - [keys objectsAtIndex:indexPath.row]; if the row is 0, we will get: "good", if 1, we will get "funny"
cell.textLabel.text = [keys objectAtIndex:indexPath.row];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return NO;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *showPickedSolution = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:showPickedSolution animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#end
The code works perfect but it doesn't push the value of the row( all the rows of plist are string type and above the there is 'Root').
I need your help to push the value to another view(DetailViewController).

make a property in your DetailViewController of the same type as of your data you want to push then use
DetailViewController *showPickedSolution = [[DetailViewController alloc]
initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
showPickedSolution.yourPropertName = [htgg objectAt:indexPath.Row];
[self.navigationController pushViewController:showPickedSolution animated:YES];
you can now access the passed dat in your DetailViewController in your property.
Please check the syntax.

I have done this in two different way depending on the scenario:
1) Delegate, when I fill some values on a form that I want to pass back to the original screen.
2) Storyboard Segue, when I want to send some value(s) to a second screen.
I usually put all the values I want in an array to send the object and then at arrival I extract what I need.
Instead of explaining each way, I can refer you to Getting Started section of the iOS Dev Site.
I hope it help!

To achieve this you can made an NSString property in class DetailViewController and set that property in didSelectRowAtIndexPath. Here is the code for your reference:
showPickedSolution.valueToBeSent = [htgg valueForKey:[[htgg allKeys] objectAtIndex:indexPath.row]];
You can use this valueToBeSent in your DetailViewController class.

Related

How to call a method in the parent view controller from a cell of a cell?

The structure of my app currently looks like this:
Collection View Controller -> Generic Cell with table view inside of it -> individual cells.
I would like to call a method in the collection view controller from one of the individual cells. So far I have implemented a delegate in the individual cell but if I can't seem to set my delegate in the collection view controller because I don't have an instance of it.
Furthermore, I have several cells inside the table view that are required to access the methods in the collection view controller.
The responder chain can help.
The view can query the responder chain for the first target that can accept a message. Suppose the message is -fooBar, then the view can query the target using the method -[UIResponder targetForAction:sender:]
// find the first responder in the hierarchy that will respond to -fooBar
id target = [self targetForAction:#selector(fooBar) sender:self];
// message that target
[target fooBar];
Note that this communication is controlled by this method:
(BOOL)canPerformAction:(SEL)action
withSender:(id)sender;
This default implementation of this method returns YES if the responder class implements the requested action and calls the next responder if it does not.
By default, the first object that responds to that message will become the target so you may want to override the canPerformAction:withSender: if needed for some views or view controllers.
For that you can do like that :
In Collection View Controller -> .h file
#interface CollectionViewController : UICollectionViewController<ColectionCellDelegate>
#end
In Collection View Controller -> .m file
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [self.collectionData count];
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CollectionCell *cell = (CollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:#"CollectionCell" forIndexPath:indexPath];
cell.cellData = [self.collectionData objectAtIndex:indexPath.row];
cell.delegate = self;
return cell;
}
-(void)tableCellDidSelect:(UITableViewCell *)cell{
NSLog(#"Tap %#",cell.textLabel.text);
DetailViewController *detailVC = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
detailVC.label.text = cell.textLabel.text;
[self.navigationController pushViewController:detailVC animated:YES];
}
In CollectionCell.h
#class CollectionCell;
#protocol ColectionCellDelegate
-(void)tableCellDidSelect:(UITableViewCell *)cell;
#end
#interface CollectionCell : UICollectionViewCell<UITableViewDataSource,UITableViewDelegate>
#property(strong,nonatomic) NSMutableArray *cellData;
#property(weak,nonatomic) id<ColectionCellDelegate> delegate;
#end
In CollectionCell.m
#implementation CollectionCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.cellData = [[NSMutableArray alloc] init];
}
return self;
}
-(void) awakeFromNib{
[super awakeFromNib];
self.cellData = [[NSMutableArray alloc] init];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.cellData count];
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"TableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.textLabel.text = [self.cellData objectAtIndex:indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[[self delegate] tableCellDidSelect:cell];
}

How to link delegate and sourcedata to only one tableview in a set of tableviews created in the mainstoryboard

Please bear with me as I am completely new at objective-c. Thanks in advance for any help you can provide!
So here is basically what I am trying to accomplish: I have 3 main tables whose contents will never change, that I therefore chose to construct in the mainstoryboard. Think of these are different grouping drilled down step by step into more and more details. So you have:
Table 1 (higher level to table 2) > Table 2 (higher level to table 3) > Table 3
Now I need to add a 4th table, but whose contents will be changed, based on a CSV file. For now I am ignoring how use CSV files and there seems to be quite a bit of info on this already. So I am electing to use Arrays using (NSArray) to store and retrive the information.
I first build the prototype of this table in the mainstoryboard so that I have an idea of what it will look like. Then I wrote the code below which ideally will update the information in table 4:
VIEWCONTROLLER.H file
#import <UIKit/UIKit.h>
#interface ViewController : UITableViewController
<UITableViewDataSource, UITableViewDelegate>
#end
VIEWCONTROLLER.M file
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
NSArray *nominalManagers;
NSArray *tipsManagers;
NSArray *tipsAmt;
NSArray *nominalAmt;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
tipsManagers = [[NSArray alloc]
initWithObjects:
#"SSG",
nil];
tipsAmt = [[NSArray alloc]
initWithObjects:
#"$tip",
nil];
nominalManagers = [[NSArray alloc]
initWithObjects:
#"Wel",
#"Gold",
#"Colch",
#"Stand",
nil];
nominalAmt = [[NSArray alloc]
initWithObjects:
#"$Wel",
#"$Gold",
#"$Colch",
#"$Stand",
nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
NSUInteger rowNum;
if (section == 0) {
rowNum = 1;
}
else if (section == 1) {
rowNum = 4;
}
else {
rowNum = 0;
}
return rowNum;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSUInteger row = [indexPath row];
NSInteger section = [indexPath section];
switch (section) {
case 0: // First cell in section 1
cell.textLabel.text = [tipsManagers objectAtIndex:[indexPath row]];
cell.detailTextLabel.text = [tipsAmt objectAtIndex:[indexPath row]];
break;
case 1: // Second cell in section 1
cell.textLabel.text = [nominalManagers objectAtIndex:[indexPath row]];
cell.detailTextLabel.text = [nominalAmt objectAtIndex:[indexPath row]];
break;
default:
cell.textLabel.text = #"WRONG SECTION";
break;
}
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
#end
My main issue is that I am unable to connect the delegate and sourcedata of this class to Table 4 (the table whose information will change). Can you help? Do you have any suggestions to better accomplish my goal?
What I understand from your question is you do not have a uitableview to connect your data source and delegate !?
I am not sure if you have added 4th viewcontroller to your storyboard yet, If not just add a UItableviewcontroller to your storyboard, create a push segue then in identity inspector choose your VIEWCONTROLLER as class name.
Add IBOutlet UITableViewto your .h file like below and in your interface builder connect your datasource and delegate. When you need refresh your tableview call [self.tableviewname reloadData];
You can use NSMutableArray to edit the items in your array, if you use NSArray array items will be static.
VIEWCONTROLLER.H file
#import <UIKit/UIKit.h>
#interface ViewController : UITableViewController
<UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, weak) IBOutlet UITableView *fourthTable;
#end
VIEWCONTROLLER.M file
#implementation ViewController
#synthesize fourthTable;
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
tipsManagers = [[NSArray alloc]
initWithObjects:
#"SSG",
nil];
tipsAmt = [[NSArray alloc]
initWithObjects:
#"$tip",
nil];
nominalManagers = [[NSArray alloc]
initWithObjects:
#"Wel",
#"Gold",
#"Colch",
#"Stand",
nil];
nominalAmt = [[NSArray alloc]
initWithObjects:
#"$Wel",
#"$Gold",
#"$Colch",
#"$Stand",
nil];
[self.fourthTable reloadData];
}

my tableview isn't being called wont crash at breakpoint

Okay so i have been fiddling with some iOS development, I am fairly new to this. Usually more of a PHP and JavaScript guy. But here is my issue... in my app I am creating that pulls a friendslist from a local xml file (it will be pointing to a php page on a server that generates a users friendslist but for simplicity sake of debugging and development I am using local.)
I have a View Controller (storybaord) that is loaded when I click "friends", as of now, the view loads but with no data, in the debugger I can see that its pulling the data from the XML, so I thought, okay maybe the TableView is never being called, so i put a breakpoint on the
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
But the app still continued to load the page,
So to recap I have a View Controller, with a search bar, toolbar and a table view, the table view has a placeholder cell given the identifier Cell, heres the coding the for friendsListTableViewController.h
//
// friendsListTableViewController.h
// #ME
//
// Created by Aaron Russell on 1/22/13.
// Copyright (c) 2013 Aaron Russell. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "TBXML.h"
#interface friendsListTableViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, NSObject>{
NSMutableArray *friendList;
TBXML * tbxml;
IBOutlet UIImage *imageFile;
}
#property (nonatomic, strong) NSMutableArray *_friends;
#property (nonatomic, strong) NSString *lname;
#end
and here is the friendsListTableViewController.m file
//
// friendsListTableViewController.m
// #ME
//
// Created by Aaron Russell on 1/22/13.
// Copyright (c) 2013 Aaron Russell. All rights reserved.
//
#import "friendsListTableViewController.h"
#interface friendsListTableViewController ()
#end
#implementation friendsListTableViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidLoad {
[super viewDidLoad];
//USED TO CONNECT TO SERVERS XML
// NSData *xmlData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:#"www.soontobesite.com"]];
//tbxml = [[TBXML alloc]initWithXMLData:xmlData];
NSString *xmlData = [[NSBundle mainBundle] pathForResource:#"friendlist" ofType:#"xml"];
NSData *data = [[NSData alloc] initWithContentsOfFile:xmlData];
tbxml = [[TBXML alloc]initWithXMLData:data];
//strings
// Obtain root element
TBXMLElement * root = tbxml.rootXMLElement;
if (root)
{
TBXMLElement * elem_PLANT = [TBXML childElementNamed:#"friend" parentElement:root];
while (elem_PLANT !=nil)
{
TBXMLElement * elem_BOTANICAL = [TBXML childElementNamed:#"fname" parentElement:elem_PLANT];
NSString *botanicalName = [TBXML textForElement:elem_BOTANICAL];
[friendList addObject:botanicalName];
elem_PLANT = [TBXML nextSiblingNamed:#"friend" searchFromElement:elem_PLANT]; //IF I CALL BREAKPOINT ON THIS LINE THE SIMULATOR BREAKS
}
//TBXMLElement *fname = [TBXML childElementNamed:#"fname" parentElement:elem_PLANT];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [friendList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [friendList objectAtIndex:indexPath.row];
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
#end
//EDIT//
I got a little further, thanks to some advice listed in the comments below before it wouldnt break at numberOfSectionsInTableView or numberOfRowsInSection but now it will break when i put a break on the numberOfSectionsInTableView or numberOfRowsInSection but still not the cellForRowAtIndexPath
You forgot to allocate the friendList instance variable. At the beginning of viewDidLoad (or even better in the initializer) do the following:
friendList = [[NSMutableArray alloc] initWithCapacity:0];
If friendList is not initialized it has a nil value. Sending a message to nil in Objective-C does not crash the application, it just does not do anything. Also see this SO question.
In your code, invoking [friendList addObject:botanicalName] simply does nothing. Later on, when you invoke [friendList count] in numberOfRowsInSection, you will return 0 (zero) to the table view. Since the table view thinks that there are no rows it will never call cellForRowAtIndexPath.

My second level detail controller is not showing the initWithObjects in my array in the viewDidLoad method

I am creating a Two Level table view. And the second view is supposed to have the list of the movies I have listed below in my viewDidLoad method, but it is not showing.(You can see my screen shots attached)Does anyone know which file where I can look to see why it is not showing? The code below is from my DisclosureButtonController.m file which is to display this information after I hit the Disclosure Buttons instance on the First Level screen.
Regards,
#import "LWWDisclosureButtonController.h"
#import "LWWAppDelegate.h"
#import "LWWDisclosureDetailController.h"
#interface LWWDisclosureButtonController ()
#property (strong, nonatomic) LWWDisclosureDetailController *childController;
#end
#implementation LWWDisclosureButtonController
#synthesize list;
#synthesize childController;
//- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
//{
// self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
//if (self) {
// Custom initialization
//}
//return self;
//}
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *array = [[NSArray alloc] initWithObjects:#"Toy Story", #"A Bug's Life", #"Toy Story 2", #"Monsters, Inc.", #"Finding Nemo", #"The Incredibles", #"Cars", #"Ratatouille", #"WALL-E", #"Up", #"Toy Story 3", #"Cars 2", #"Brave", nil];
self.list = array;
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.list = nil;
self.childController = nil;
// Release any retained subviews of the main view.
}
#pragma mark -
#pragma mark Table Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [list count];//
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString * DisclosureButtonCellIdentifier = #"DisclosureButtonCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DisclosureButtonCellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:DisclosureButtonCellIdentifier];
}
NSUInteger row = [indexPath row];
NSString *rowString = [list objectAtIndex:row];
cell.textLabel.text = rowString;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
#pragma mark -
#pragma mark Table Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Hey, boss do you see the disclosure button?" message:#"If you're trying to drill down, touch that instead mate!" delegate:nil cancelButtonTitle:#"Won't happen again" otherButtonTitles:nil];
[alert show];
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath: (NSIndexPath *)indexPath
{
if (childController == nil)
{
childController = [[LWWDisclosureDetailController alloc]initWithNibName:#"LWWDisclosureDetail" bundle:nil];
}
childController.title = #"Disclosure Button Pressed";
NSUInteger row = [indexPath row];
NSString *selectedMovie = [list objectAtIndex:row];
NSString *detailMessage = [[NSString alloc]initWithFormat:#"You pressed the disclosure button for %#.", selectedMovie];
childController.message = detailMessage;
childController.title = selectedMovie;
[self.navigationController pushViewController:childController animated:YES];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Why not start the array with the VC instead of after the VC loads? Try overriding the init method.
- (id) init {
if((self == [super init])) {
//load your array here and set
}
return self;
}
That way, on older devices, you don't have to wait after the view loads to see the array. But, however, this is my preference. I love to override init methods and create my own.
Also, for some weird reason on my SDK, I have to use NSMutableArray instead of NSArray or it won't work. Maybe you have the same issue?
Also, I've noticed NSString *selectedMovie. Instead of using just "row", use the getter indexPath.row.
Hope these suggestions helped!
Call:
[self.tableView reloadData]; (put in place of self.tableView the var or property connected to the tableView)
after the:
self.list = array;
code line in the viewDidLoad method
and put a
NSLog(#"number of rows: %d", [self.list count]);
in
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [list count];//
}
to check if it is not zero.

TableView is delegated, populates but wont group

I am having a weird problem and i couldn`t find a solution (or something similar).
The thing is, my UITableView Populates with initial info (for testing), but no matter what i do i can't seem to put it to grouped style (i can select it on the UI but it wont show)
I initially started a TabBar project and added a third navigationController view in the tabs.
#import <UIKit/UIKit.h>
#interface RootViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *tableData;
}
#property (nonatomic, retain) NSMutableArray *tableData;
-(void)initTableData;
#end
This is the header, and as you can see it has nothing out of the ordinary. The following code is inside the .m file of the header i just posted(ill only be posting uncommented code:
#synthesize tableData;
-(void)initTableData
{
tableData = [[NSMutableArray alloc] init];
[tableData addObject:#"Cidade"];
[tableData addObject:#"Veículo"];
[tableData addObject:#"Ano"];
[tableData addObject:#"Valor"];
[tableData addObject:#"Cor"];
[tableData addObject:#"Combustível"];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Busca";
UIBarButtonItem *_backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.backBarButtonItem = _backButton;
[self initTableData];
}
- (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 6;
}
// Customize the appearance of table view cells.
- (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] autorelease];
}
// Configure the cell...
cell.textLabel.text = [tableData objectAtIndex:[indexPath row]];
return cell;
}
- (void)dealloc {
[tableData release];
[super dealloc];
}
Nothing out of the ordinary again as you can see...
Any idea of what may be causing this? I tried
- (id)initWithStyle:(UITableViewStyle)style {
// Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
// Custom initialization.
}
return self;
}
because i don't know what else to do. (also didn't worked)
Again, i got the delegate and datasource set to File`s Owner.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Set the tab bar controller as the window's root view controller and display.
self.window.rootViewController = self.tabBarController;
RootViewController *rvc = [[RootViewController alloc] initWithStyle: UITableViewStyleGrouped];
[self.window makeKeyAndVisible];
return YES;
}
As you have modified the - (id)initWithStyle:(UITableViewStyle)style initializer to return a UITableView with a grouped style, do you call this initializer when you initialize the RootViewController?
RootViewController *rvc = [[RootViewController] alloc] initWithStyle: UITableViewStyleGrouped];
Grouped tables respond to the sections. You only have 1 section listed so you will only see the 1 group. Try and add a 2nd tableData for the 2nd group and return 2 sections. You will also have to split the data in your -cellForRowAtIndexPath by section as well to make sure the data goes to the right section.
if (indexpath.section == 0) {
// first section and first tableData
}
if (indexpath.section == 1) {
// second section and second tableData
}