Can't find self.tableview after accidental delete - objective-c

I accidentally deleted my firstviewcontroller.m file which is for a view that has a tableview. I then re-created my .m file the best I can remember and thought I had it all correct.
I'm now getting an error on the line
[self.tableview refresh];
saying self.tableview isn't found.
I can't seem to figure out what I missed. I don't want to overwhelm you guys with code so I'll try to just post the pertinent part.
#import "FirstViewController.h"
#import "Shared.h"
#import "Message.h"
#implementation FirstViewController
-(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];
}
Message *msg = [[Shared sharedInstance].messages objectAtIndex:indexPath.row];
NSString *authorName = msg.authorName;
NSString *cellValue = authorName;
cell.textLabel.text = cellValue;
return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
`return [[Shared sharedInstance].messages count];`
}
-(void) viewWillAppear:(BOOL)animated {
`[self.tableView reloadData];`
}

tableView is a property of UITableViewController. Double-check that you inherited FirstViewController from a UITableViewController, but not from UIViewController.

Related

Can't identify if tableView parameter is seachResultsTableView

I'm developing an iOS 6 application.
I want to add a search functionallity to a UITableView following this Table Search example from Apple. But, my implementation doesn't work.
On - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath I don't know how to if tableView parameter is searchDisplayController.searchResultsTableView.
Take a look at this screenshot:
As you can see, tableView is a UISearchResultsTableView, but this line:
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView])
It is always false.
Maybe, I need to add an outlet to access seachResultsTableView, but I haven't found that outlet on TableSearch example.
This ViewController.h:
#interface ViewController: UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchDisplayDelegate, UISearchBarDelegate> {
What do I need to do to make it work?
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if(searching)
{
cell.textLabel.text = [searchedListOfItems objectAtIndex:indexPath.row];
}
else {
NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
cell.textLabel.text = [dictionary objectForKey:#"Title"];
}
return cell;
if all you're trying to achieve is a normal search then use this.

populate uitableview as subview of uicollectionview

I have a problem in populating a uitableview created programmatically as a subview of uicollectionview.
here's my code:
.h file
#import <UIKit/UIKit.h>
#interface TheViewController : UICollectionViewController<UITableViewDataSource>
#property (atomic,strong) IBOutlet UIBarButtonItem *plus;
-(IBAction) addTeamPressed:(id)sender;
#end
.m file
- (void)viewDidLoad
{
[super viewDidLoad];
NSError *jsonParsingError = nil;
teamsView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.window.bounds.size.width, self.view.window.bounds.size.height)];
NSData *t = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://..."] options:NSDataReadingMappedIfSafe error:&jsonParsingError];
NSArray *tmp = [NSJSONSerialization JSONObjectWithData:t options:0 error:&jsonParsingError];
UITableViewCell * teamCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
teams = [[NSMutableArray alloc]init];
[teamsView addSubview:teamCell];
teamsView.delegate = self;
teamsView.dataSource = self;
for(int i =0;i<tmp.count;i++){
[teams addObject:[tmp objectAtIndex:i]];
}
plus.title = #"Done";
[self.view addSubview:teamsView];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
// If You have only one(1) section, return 1, otherwise you must handle sections
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [teams count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
TeamsNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[SquadreNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.name.text = [NSString stringWithFormat:#"%#",[[teams objectAtIndex:indexPath.row] objectForKey:#"name"]];
NSLog(#"%d teams",[teams count]);
return cell;
}
my tableview is displaying correctly, of course thanks to other functions not mentioned here, but no rows in the subview.
the compiler prints out the number of teams, so it calls the delegate methods, but it doesn't populate the subview...
what 's wrong with this?
thanks in advance
Dario
First you should remove this from your viewDidLoad:
UITableViewCell * teamCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
[teamsView addSubview:teamCell];
Then make sure that your teams array is retained, just put this line to the header file:
#property (nonatomic, strong) NSMutableArray* teams;
And then use self.teams instead of just teams
At the end of viewDidLoad method you should call [teamsView reloadData];
Also it would be good to change your table data source method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
TeamsNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
// please make sure that you allocate required object here
cell = [[TeamsNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// please make sure that you do have `name` property in your `TeamsNameCell` class
cell.name.text = [NSString stringWithFormat:#"%#",[[teams objectAtIndex:indexPath.row] objectForKey:#"name"]];
NSLog(#"%d teams",[teams count]);
return cell;
}
Please make sure that your TeamsNameCell class has name property in the header:
#property(nonatomic, strong) UILabel* name;
And also do not forget to allocate this label in the constructor of TeamsNameCell:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.name = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,50)];
[self addSubview:self.name];
}
return self;
}
Or Maybe it is better to use default cell's label:
instead of cell.name.text = #"text"; use cell.textLabel.text = #"text";
solution is,
static NSString *CellIdentifier = #"Cell";
TeamsNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[SquadreNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.labelText.text = [NSString stringWithFormat:#"%#",[[teams objectAtIndex:indexPath.row] objectForKey:#"name"]];
NSLog(#"%d teams",[teams count]);
return cell;

Return a custom cell from cellForRowAtIndexPath:

I ran into a trouble and I'm looking for help.
I need to save a identifier in a cell, so I did a subclass
of UITableViewCell and added to it the identifier property. In my view
controller I create the cells as follow:
- (SidebarCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* tableViewCell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
SidebarCell * cell = (SidebarCell *)tableViewCell;
Category *category = [categoryDataController.categories objectAtIndex:indexPath.row];
cell.textLabel.text = category.name;
if (category.checked == 1) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
The problem is that when I select one of the cells, the method
cellForRowAtIndexPath: returns an UTableViewCell object instead
of a SidebarCell, so I don't have access to the identifier property:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO];
SidebarCell *cell = (SidebarCell *)[tableView cellForRowAtIndexPath:indexPath]; // Trying to cast from UITableViewCell to SidebarCell
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[CategoryDataController updateRow:cell.identifier status:1]; // Here the app crashes
} else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
[CategoryDataController updateRow:cell.identifier status:0];
}
}
is there a way for cellForRowAtIndexPath: to return an Sidebarcell object?
Thanks beforehand.
Edit
The SidebarCell.h
#import <UIKit/UIKit.h>
#interface SidebarCell : UITableViewCell
#property (nonatomic) NSInteger identifier;
#end
SidebarCell.m:
#import "SidebarCell.h"
#implementation SidebarCell
#synthesize identifier;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
The error:
2012-04-24 09:21:08.543 Dongo[2993:11603] -[UITableViewCell identifier]: unrecognized selector sent to instance 0x6d87d50
2012-04-24 09:21:08.571 Dongo[2993:11603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell identifier]: unrecognized selector sent to instance 0x6d87d50'
*** First throw call stack:
(0x14b1052 0x1a65d0a 0x14b2ced 0x1417f00 0x1417ce2 0x116bc 0x48e71d 0x48e952 0xd1686d 0x1485966 0x1485407 0x13e87c0 0x13e7db4 0x13e7ccb 0x239d879 0x239d93e 0x3fea9b 0x2e85 0x2715 0x1)
terminate called throwing an exception(lldb)
You should leave the tableView:cellForRowAtIndexPath: method signature intact: it should return (UITableViewCell *) and not your custom cell class. Since your SidebarCell is a subclass of UITableViewCell, it should all "just work". No need to call super or any of that:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
SidebarCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if (cell == nil) {
cell = [[SidebarCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"CellIdentifier#"];
}
// ... set up the cell here ...
return cell;
}
In other methods, the cast should work just as you have it:
SidebarCell *cell = (SidebarCell *)[tableView cellForRowAtIndexPath:indexPath];
UITableViewCell* tableViewCell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
The above line of code must be returning a UITableViewCell and not a SidebarCell.
SidebarCell * cell = (SidebarCell *)tableViewCell;
Then on your second line you cast the cell to a SidebarCell, but you are simply lying to the compiler and telling it that it should be a SidebarCell when it is in fact not. When you cast something it needs to start out as the correct type.
Instead of those two lines, you should be creating an actual SidebarCell. How you do this depends on how you have defined it.
Let me know if it is in a storyboard, xib, or created in code and I can provide an example if needed.
EDIT
Since you are creating the cell in code, you simply need to replace the first two lines in cellForRowAtIndexPath with this:
SidebarCell * cell = [[SidebarCell alloc] init];
Everything else looks like it should work as-is.
In my case i have used custom cell of a class tempTableViewcell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = #"cellidentifier";
tempTableViewcell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = #"abc";
return cell;
}

Prepareforsegue navigationItem.title one behind

I'm still relative new to object c, so please bear over with me if this is a rookie question. I'm trying to set the title of my navigationcontroller with the corresponding object information. I'm using prepareforsegue for it, but the first time is segue to the new controller, the title is blank. If i try again, it shows up, but if i pressed something else, it shows the title of the things i pressed the time before. I have embedded my code below.
//.h
#import <UIKit/UIKit.h>
#interface STATableViewController : UITableViewController
#property(strong,nonatomic)NSArray *listOfExercises;
#property(weak,nonatomic)NSString *navTitle;
#end
//.m
#import "STATableViewController.h"
#import "ExercisesViewController.h"
#implementation STATableViewController
#synthesize listOfExercises = _listOfExercises, navTitle = _navTitle;
- (void)viewDidLoad
{
[super viewDidLoad];
_listOfExercises = [NSArray arrayWithObjects:#"Raketstart",#"SpeedBåd",#"Træstamme",nil];
self.navigationItem.title = #"Exercises";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [_listOfExercises 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];
}
NSString *cellValue = [_listOfExercises objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_navTitle = [_listOfExercises objectAtIndex:indexPath.row];
//NSLog(_navTitle);
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:#"toExercise"])
{
ExercisesViewController *foo = [segue destinationViewController];
foo.navigationItem.title= _navTitle;
}
}
#end
This is happening because prepareForSegue:sender: is being called before tableView didSelectRowAtIndexPath:. So you are always setting your navigationItem's title before you are setting your _navTitle property with the value you want.
Instead of getting the title in didSelectRowAtIndex path, do it in your prepareForSegue like this:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:#"toExercise"])
{
// "sender" is the table cell that was selected
UITableViewCell *cell = (UITableViewCell*)sender;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
NSString *title= [_listOfExercises objectAtIndex:indexPath.row];
ExercisesViewController *foo = [segue destinationViewController];
foo.navigationItem.title = title;
}
}

Method Definition Not In #implementation Context: Error

When I try to import it gives error at the import stating "method definition not in #implementation context". I think the error lies within the following code...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *infoDictionary = [self.jsonArray objectAtIndex:indexPath.row];
static NSString *Prospects = #"agencyname";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Prospects];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:Prospects] autorelease];
}
// setting the text
cell.text = [infoDictionary objectForKey:#"agencyname"];
// Set up the cell
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ViewAgencyController *dvController = [[ViewAgencyController alloc] initWithNibName:#"ViewAgency" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
dvController = nil;
}
Cant Figure it out.
All objective-C method definitions must be between #implemention and #end compiler directives. Without those, there's no way for the compiler to know what class you want the method to belong to.
Look at your header and make sure you have an #end directive to close your class declaration, and look at your .m file and make sure you have an #implementation directive before, and an #end after your method implementations.