Multiple tableview issue - objective-c

I have implemented a view controller with a table view controll called ligatable. This table view has its delegate in a table view controller called ligaViewController.h. Table view control has implemented one prototype cell, that has set ligaCell.h controller. Here are the details:
competicionViewController.h
#import "ligaViewController.h"
#interface CompeticionViewController : UIViewController
{
IBOutlet UITableView *ligaTable;
ligaViewController *ligaController;
}
#property (strong, nonatomic) IBOutlet UITableView *ligaTable;
#property (strong, nonatomic) ligaViewController *ligaController;
competicionViewController.h
- (void)viewDidLoad
{
...
ligaController = [[ligaViewController alloc] init];
[ligaTable setDelegate:ligaController];
[ligaTable setDataSource:ligaController];
....
And the delegate method in ligaViewController:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ligaCell *cell = [self.tableView
dequeueReusableCellWithIdentifier:#"posicion"];
NSDictionary *posicion = [[NSDictionary alloc] initWithDictionary: [ligaArray objectAtIndex:indexPath.row]];
cell.posicion.text = [posicion valueForKey:#"posicion"];
cell.PJ.text = [posicion valueForKey:#"pj"];
cell.PG.text = [posicion valueForKey:#"pg"];
cell.PP.text = [posicion valueForKey:#"pp"];
cell.PF.text = [posicion valueForKey:#"favor"];
cell.PC.text = [posicion valueForKey:#"contra"];
return cell;
}
It is currently giving me this error:
terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
Many thanks

Use -
ligaCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"posicion"];
if (cell == nil)
{
cell = [[[ligaCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CustomCellIdentifier] autorelease];
}

Related

change static UITableViewCell background color inside a UIPopoverController xcode5

I'm developing an app for iOS7.1 in Xcode 5
And I want to change tableview cell's background color at the final row to look like this
but when I call this UITableViewController inside a UIPopoverController I get this:
Somehow my tableview cell lost it's original background color
this is my code:
//iPad Popover Section
if (!resultBTPopover || !resultBTPopover.popoverVisible)
{
ResultadosBalanceTransferVC *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"ResultadosBalanceTransferVC"];
//controller.delegate = self;
controller.title = #"Resultados";
controller.amountValue = self.amountTextfield.text;
controller.promoRateValue = self.promoRateTextfield.text;
controller.normalRateValue = self.normalRateTextfield.text;
controller.otherBankRateValue = self.otherBankRateTextfield.text;
controller.termValue = self.termTextfield.text;
navController = [[UINavigationController alloc]initWithRootViewController:controller];
navController.toolbarHidden = FALSE;
NSDictionary *textAtributesDictionaryTitle = [NSDictionary dictionaryWithObjectsAndKeys:
UIColorFromRGB(toolbarTintColor), NSForegroundColorAttributeName,
[UIFont fontWithName:#"HelveticaNeue-BoldCond" size:19.0f], NSFontAttributeName,
nil];
[navController.navigationBar setTitleTextAttributes:textAtributesDictionaryTitle];
resultBTPopover = [[UIPopoverController alloc] initWithContentViewController:navController];
[resultBTPopover presentPopoverFromRect:CGRectMake(self.view.center.x - (self.view.center.x * .2734), self.view.center.y - (self.view.center.y * .6510), 300, 400)
inView:self.view permittedArrowDirections:0
animated:YES];
}
else{
[resultBTPopover dismissPopoverAnimated:YES];
resultBTPopover = nil;
}
}
how to get my original tableview cell colors???
thanks in advance for the support
EDIT: I'll put method proposed by Plokstorm
ResultsCell.h:
#import <UIKit/UIKit.h>
#interface ResultsCell : UITableViewCell
#property (nonatomic, weak) IBOutlet UILabel *label1;
#property (nonatomic, weak) IBOutlet UILabel *label2;
#property (nonatomic, weak) IBOutlet UILabel *label3;
#property (nonatomic, weak) IBOutlet UILabel *label4;
#property (nonatomic, weak) IBOutlet UILabel *label5;
#end
on STORYBOARD I did this:
all of them changed to ResultsCell custom class
linked all the properties with visual labels of the UITableViewCell
on CODE I did this for testing:
ResultadosBalanceTransferVC.m
#import "ResultsCell.h"
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
ResultsCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
cell = [[ResultsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
if (indexPath.row == 2){
cell.backgroundColor = [UIColor redColor];
cell.label1.text = #"Pago Total";
}
return cell;
}
and... still getting same result:
Show us your
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
You can change a cell background with cell.backgroundColor.
Try that:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
if (indexPath.row == 2)
cell.backgroundColor = [UIColor redColor];
return cell;
}
You need to change cell class with it in storyboard.
If you don't create a cell class, you should create an object which extends UITableViewCell. Then you can add its property.
#interface ResultsCell : UITableViewCell
#property (nonatomic, weak) IBOutlet UILabel *lYourFirstLabel;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
ResultsCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
cell = [[ResultsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
if (indexPath.row == 2)
cell.backgroundColor = [UIColor redColor];
[cell.lYourFirstLabel setText:#"hello"];
return cell;
}

Null data when loading DetailView

I have UITableView I want to click on row and load detail view but I have null information in my Log in detail view
would you please help m win this implementation
Thanks in advance!
cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
//Load Cell for reuse
static NSString *CellIdentifier = #"TestCell";
TestCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell =[ [[NSBundle mainBundle] loadNibNamed:#"TestCell" owner:nil options:nil]
lastObject];
}
_tests = [server info];
_t = [NSMutableString stringWithFormat:#"%# ", [_tests objectAtIndex:indexPath.row]];
NSString *testdata = _t;
_components = [testdata componentsSeparatedByString:#","];
for (NSString *aComponent in _components) {
if ([aComponent hasPrefix:#"titletest"]) {
_subComponents = [aComponent componentsSeparatedByString:#":"];
_testString = [_subComponents[1] stringByTrimmingCharactersInSet:[NSCharacterSet
characterSetWithCharactersInString:#"\""]];
}if ([aComponent hasPrefix:#"note"]){
_subComponents = [aComponent componentsSeparatedByString:#":"];
_noteString = [_subComponents[1] stringByTrimmingCharactersInSet:[NSCharacterSet
characterSetWithCharactersInString:#"\""]];
break;
}
}
cell.title.text = _testString;
cell.note.text = _noteString;
return cell;
}
didSelectRowAtIndexPath method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
TestDetailView *c = [[TestDetailView alloc] init];
///I don't know what should I put here to c
[self.navigationController pushViewController:c animated:YES];
}
but in My detail view
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:YES];
NSLog(#" test : %#",_Info.datas); ==>> my log was (null)
}
My Detail View Header
#property (strong, nonatomic) IBOutlet UILabel *bDetailTitle;
#property (strong, nonatomic) IBOutlet UILabel *bDetailNote;
#property (nonatomic, strong) NSArray *datas;
#property (nonatomic,strong) TestTable *Info;
#end
I'm assuming that TestDetailView is a ViewController subclass and that you have a property on it possibly called datas. If that is the case you appear to create your detail view controller in didSelectRowAtIndexPath: like this:
TestDetailView *c = [[TestDetailView alloc] init];
But then immediately change it the pointer to something else (possibly nil):
c = [_datas objectAtIndex:indexPath.row];
objectAtIndex returns an id which means your code probably doesn't throw a warning in Xcode, but at run time if [_datas objectAtIndex:indexPath.row] does not return a UIViewController (and I suspect that it does not) your app could crash. If it is not throwing an error or crashing you possibly have another problem in that _datas is nil in your parent view controller.
It is a very common technique to pass values to view controllers I would recommend reading up on how to do this. This type of question has been asked on S.O. many times:
How to set value for NSString in another view controller from one viewcontroller

Adding UITableView to existing UIViewController

I have a UIViewController which open up a Camera, after doing the imagePickerController:didFinishPickingMediaWithInfo: a UITableView shall be shown.
Somehow the tableView delegation code is not triggered. A tableView is shown on my iPhone but with empty rows. Seems that just the IB Stuff is displayed without the code. But I cannot figure out what I missed.
How do I get the UITableView working?
MainStoryboard.storyboard
Camera View Controller Scene
----------------------------
Camera View Controller
View
Image View // hooked up with .h
Table View // hooked up with .h
Table View Cell - clocation
CameraViewController.h
#import <UIKit/UIKit.h>
#interface CameraViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITableViewDataSource, UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UIImageView *imageView; // hooked up with IB UIImageView
#property (strong, nonatomic) NSMutableArray *locations;
#property (strong, nonatomic) IBOutlet UITableView *tableView; // hooked up with IB UITableView
#end
CameraViewController.m (Updated 20130725 with working solution)
#import "CameraViewController.h"
#interface CameraViewController ()
#end
#implementation CameraViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// self.locations = [[NSMutableArray alloc] init];
// is filled with content when photo is taken. Means has content when tableview shall be shown.
self.locations = #[ #"foo", #"bar" ]; // dummy setup for this thread
// Do any additional setup after loading the view.
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
// select Photo
} else {
[self takePhoto];
}
[self initTableView];
}
// [...] Camera Stuff
#pragma mark - table view things
-(void)initTableView {
_tableView.dataSource = self;
_tableView.delegate = self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.locations.count; // a breakpoint is configured but never get hit...
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"clocation";
// Not working in this case:
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// You have to use this one:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:#"Empty Cell %d", indexPath.row];
return cell;
}

Passing data back :Delegate method not called

I am working on an app that connects with google docs using oauth 2.0, once the user has connected, He clicks a button that goes to a tableView to show all the documents. For this I am using a segue and passing data forward. Now I want the user to select one document, put a checkmark on it and return to the viewcontroller passing data back and display wich document was selected. I read that for passing data back I needed to use a protocol and a delegate. I followed this: Passing Data between View Controllers but my problem is that my delegate method is not being called.
Here is my storyboard, just 2 views, a viewcontroller and a tablaviewvcontroller called VistaTableViewController.
When the user pushes the authenticate button, He connects with google docs using oauth. When he pushes the "Listar" button the VistaTableViewController appears.
Here is the code of VistaTableViewController.h where a define the protocol:
#import <UIKit/UIKit.h>
#import "GData.h"
#class VistaTableViewController;
#protocol VistaTableViewControllerDelegate <NSObject>
- (void)loqueselecciono:(VistaTableViewController *)controller didSelectDoc:(NSString *)documento;
#end
#interface VistaTableViewController : UITableViewController <UITableViewDataSource>
{
IBOutlet UITableView *tablalistar;
GDataFeedDocList *mDocListFeed2;
}
#property (nonatomic, weak) id <VistaTableViewControllerDelegate> delegate;
#property (nonatomic, strong) NSString *documento;
#property (nonatomic, retain) GDataFeedDocList *mDocListFeed2;
#end
Here is the code of VistaTableViewController.m where I call the delegate method:
#import "VistaTableViewController.h"
#implementation VistaTableViewController
{
NSInteger selectedIndex;
}
#synthesize delegate;
#synthesize documento;
#synthesize mDocListFeed2;
- (void)viewDidLoad
{
[super viewDidLoad];
selectedIndex = [[mDocListFeed2 entries] indexOfObject:self.documento];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[mDocListFeed2 entries] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"tablalistar"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"tablalistar"];
}
GDataEntrySpreadsheet *doc = [[mDocListFeed2 entries] objectAtIndex:indexPath.row];
cell.textLabel.text = [[doc title] stringValue];
if (indexPath.row == selectedIndex)
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (selectedIndex != NSNotFound)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:selectedIndex inSection:0]];
cell.accessoryType = UITableViewCellAccessoryNone;
}
selectedIndex = indexPath.row;
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
GDataEntrySpreadsheet *doc = [[mDocListFeed2 entries] objectAtIndex:indexPath.row];
NSString *theDoc = [[doc title] stringValue];
[self.delegate loqueselecciono:self didSelectDoc:theDoc];
}
#end
Ok, so now I just need to tell ViewController to import VistaTableViewController and conform to its protocol.
here is the code of ViewController.h
#import <UIKit/UIKit.h>
#import "GTMOAuth2ViewControllerTouch.h"
#import "GData.h"
#import "VistaTableViewController.h"
#interface ViewController : UIViewController <VistaTableViewControllerDelegate>
- (GDataServiceGoogleDocs *)docsService;
- (void)authorize;
- (void) mifetch;
- (IBAction)autenticarse;
- (IBAction)listar:(id)sender;
- (void) ticket: (GDataServiceTicket *) ticket finishedWithFeed: (GDataFeedDocList *) feed error: (NSError *) error;
#property (nonatomic, retain) NSString *accessToken;
#property (nonatomic, retain) GDataFeedDocList *mDocListFeed;
#property (nonatomic, retain) GDataServiceTicket *mDoclistFetchTicket;
#end
Then in Viewcontroller.m I implement the delegate method as follows: (I just want to display an alert showing the title of de document that was selected).
- (void)loqueselecciono:(VistaTableViewController *)controller didSelectDoc:(NSString *)documento;
{
[self.navigationController popViewControllerAnimated:YES];
UIAlertView *alertView = [ [UIAlertView alloc] initWithTitle:#"doc seleccionado"
message:[NSString stringWithFormat:#"titulo: %#", documento]
delegate:self
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil];
[alertView show];
}
and in Viewcontroller.m as well I wrote this to assign self to the delegate.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"displaydocs"])
{
VistaTableViewController *vistatableview = [[VistaTableViewController alloc] init];
vistatableview.delegate = self;
vistatableview = [segue.destinationViewController performSelector:#selector(setMDocListFeed2:) withObject:mDocListFeed];
}
}
Ok, so I get de tableView displaying all the documents and the user can select one showing de checkmark but the delegate method is not being called so It does not return to the viewcontroller view and no data is passed back.
What am I missing?? Thanks!!
When you write VistaTableViewController *vistatableview = [[VistaTableViewController alloc] init]; you are creating a new object. What you really want to do is use the segue.destinationViewController, which should be a VistaTableViewController and set its delegate to self.
As is, the object with the delegate is not the object being pushed and handling the view logic.
(Also, setMDocListFeed2 doesn't return an object so the assignment on performSelector is rather misleading.)

How does a table view controller communicate with its detail view?

I have a table view, and if a certain row is tapped, the detail view will show up. But how does the detail view know which row was tapped ?
So for instance, I have a MainController which displays names. If I tap "Johnny" The next screen should show a label with the string "Johnny". How would I do that ?
[EDIT - Added code]
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ArticleView *article = [self.storyboard instantiateViewControllerWithIdentifier:#"ArticleView"];
[article.myLabel setText:#"random"];
[self.navigationController pushViewController:article animated:YES];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSDictionary *info = [json objectAtIndex:indexPath.row];
cell.textLabel.text = [info objectForKey:#"username"];
cell.textLabel.backgroundColor = nil;
cell.textLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.backgroundColor = nil;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.text = [info objectForKey:#"user_pic"];
// Configure the cell...
return cell;
}
ArticleView.h
#interface ArticleView : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *myLabel;
#end
ArticleView.m
-> Synthesizing properties
You could pass a object (i.e. the String Jonny) to the detail view controller as a property.
#interface ArticleViewController : UIViewController
#property (retain) ArticleView *articleView;
#end
//Don't forget to synthesize name
in the tableview controller
-(void)tableView:(UITableView *)tableview didSelectRowAtIndexPath:(NSIndexPath)indexPath
{
NSDictionary *info = [json objectAtIndex:indexPath.row];
ArticleViewController *articleViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"ArticleViewController"];
articleViewController.articleView = articleView;
[self.navigationController pushViewController: articleViewController animated:YES];
}
the table view delegate has the method tableView:didSelectRowAtIndexPath:, in this method you present your detail view so you know which row was selected from the indexPath parameter and can pass any info you need when you create the new view controller
Assuming you're talking iPhone here, I would have a property name on my detail view:
#propery (nonatomic, retain) NSString * name;
and an init method as follows:
- (id) initWithName: (NSString *) name
{
self = [super initWithNibName: #"<view-controller-nib-name>" bundle: nil];
if (self) {
self.name = name;
}
return self;
}
and then set name label in viewDidLoad
In the tableView:didSelectRowAtIndexPath: method, get the name referenced by the index path, create and init a detail view controller using initWithName:, then push on the nav stack.