Getting EXC_BAD_ACCESS when i'm select row in UITableView - objective-c

I have problem during selection rows in my table.
When I select any row or try to scroll the table I'm getting EXC_BAD_ACCESS.
My view is very simple.
After the user press on button, I create table with 10 objects (Food class). Food is a simple class with id and name. I have an NSMutableArray that conatians all the obejcts.
Thanks for your help!
The code:
#interface SearchFood : UIViewController <UITextFieldDelegate,
UITableViewDataSource, UITableViewDelegate>
{
UITableView * foodsTable;
}
#property(nonatomic, retain) NSMutableArray *FoodsArray;
-(void)searchButtonClick:(id)sender;
#end
*************************************
import "SearchFood.h"
import "AppDelegate.h"
import "Food.h"
#implementation SearchFood
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Search for food";
self.view.backgroundColor = [UIColor whiteColor];
btnSearch = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[btnSearch setFrame:CGRectMake(220, 10,100, 40)];
[btnSearch setTitle:#"Serach" forState:UIControlStateNormal];
[btnSearch addTarget:self action:#selector(searchButtonClick:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:btnSearch];
[btnSearch release];
Food *newfood = [Food new];
newfood.Food_Name = #"guy";
self.FoodsArray = [NSMutableArray arrayWithObjects:newfood, nil];
}
-(void)searchButtonClick:(id)sender
{
foodsTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 80, 320, 460)
style:UITableViewStylePlain];
foodsTable.dataSource = self;
foodsTable.delegate = self;
foodsTable.allowsSelection = YES;
[self.view addSubview:foodsTable];
[foodsTable release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.FoodsArray count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath {
NSLog(#"Press row");
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"<#MyCell#>";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
if (!(indexPath.row >= [self.FoodsArray count]))
{
Food * currFood = (Food *)[self.FoodsArray objectAtIndex:indexPath.row];
cell.textLabel.text = currFood.Food_Name;
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
}
return cell;
}
*********************
Food class:
#interface Food : NSObject
#property (nonatomic, retain) NSString* Food_ID;
#property ( nonatomic, retain ) NSString * Food_Name;
#end
#import "Food.h"
#implementation Food
#synthesize Food_ID;
#synthesize Food_Name;
#end

If the cell is not initiated a first time, dequeueReusableCellWithIdentifier: returns nil. Thus, you can't go ahead with a non-existing cell.

Related

UITableView is not showing the content of NSArray

I am making an app which involves UITableView. I have added the TableView to the viewController.
This is my ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *tableview;
#end
And following is my ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
{
NSArray *tabledata;
}
#synthesize tableview;
- (void)viewDidLoad
{
[super viewDidLoad];
tabledata = [NSArray arrayWithObjects:#"Egg Benedict", #"Mushroom Risotto", #"Full Breakfast", #"Hamburger", #"Ham and Egg Sandwich", nil];
tableview.delegate = self;
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [tabledata count];
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 150.0;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [tabledata objectAtIndex:indexPath.row];
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
When i am running this program it is showing me the table but it is not having any data and also the number of rows is not equal to 5. Please somebody help me.
Thanks in advance
modified code from viewdidload
tabledata = [NSArray arrayWithObjects:#"Egg Benedict", #"Mushroom Risotto", #"Full Breakfast", #"Hamburger", #"Ham and Egg Sandwich", nil];
tableview.delegate = self;
tableview.dataSource = self;

Data is unexpectadely reset back to 0 when passed to a new View Controller

My lensInt(an integer) get reset back to 0 when i pass lensInt =1 to my ViewController1( This is a table view) at from MenuViewController. My Menu View Controller is a facebook like sliding background . Also, My ViewController1 is embeded in a Navigation Controller Bar. The Integer Only reset when it passes the #implementation TableViewController(I uses breakpoint to debug it).
My ViewController1.h:
#import <UIKit/UIKit.h>
#interface ViewController1 : UITableViewController{
NSMutableArray *ACUVUE;
NSMutableArray *BAUSCH_LOMB;
int lensInt;
}
My ViewController1.m:
#import "ViewController1.h"
#implementation ViewController1
#synthesize lensInt;
- (void)viewDidLoad
{
ACUVUE =[[NSMutableArray alloc]
initWithObjects:#"1-DAY ACUVUE DEFINE",#"1-DAY ACUVUE ASTIGMATISM", nil];
BAUSCH_LOMB=[[NSMutableArray alloc]
initWithObjects:#"B+L PURE VISION",#"B+L PURE VISION MULTIFOCAL",#"B+L PURE VISION TORIC",nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(self.lensInt == 0){
return [ACUVUE count];}
if(self.lensInt == 1){
return [BAUSCH_LOMB count];}
[self.tableView reloadData];}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; }
// Configure the cell...
if(self.lensInt == 0){
cell.textLabel.text = [ACUVUE objectAtIndex:indexPath.row];}
if(self.lensInt == 1){
cell.textLabel.text = [BAUSCH_LOMB objectAtIndex:indexPath.row];}
//---------- CELL BACKGROUND IMAGE -----------------------------
UIImageView *imageView = [[UIImageView alloc] initWithFrame:cell.frame];
UIImage *image = [UIImage imageNamed:#"LightGrey.png"];
imageView.image = image;
cell.backgroundView = imageView;
[[cell textLabel] setBackgroundColor:[UIColor clearColor]];
[[cell detailTextLabel] setBackgroundColor:[UIColor clearColor]];
//Arrow
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;}
#end
My MenuViewController.m:
#import "MenuViewController.h"
#import "ECSlidingViewController.h"
#import "ViewController1.h"
#interface MenuViewController ()
#property (strong, nonatomic) NSArray *menu;
#property (strong, nonatomic) NSArray *section1;
#property (strong, nonatomic) NSArray *section2;
#property (strong, nonatomic) ViewController1 *lens;
#end
#implementation MenuViewController
#synthesize lens;
#synthesize menu, section1, section2;
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
tableView.backgroundColor = [UIColor darkGrayColor];
cell.contentView.backgroundColor = [UIColor darkGrayColor];
cell.textLabel.textColor =[UIColor whiteColor];
tableView.separatorColor =[UIColor blackColor];}
- (void)viewDidLoad
{ [super viewDidLoad];
self.section1 = [NSArray arrayWithObjects:#"Acuvue", #"Bausch + Lomb", nil];
self.section2 = [NSArray arrayWithObjects:#"Daily", #"Monthly", nil];
self.menu = [NSArray arrayWithObjects:self.section1, self.section2, nil];
[self.slidingViewController setAnchorRightRevealAmount:190.0f];
self.slidingViewController.underLeftWidthLayout = ECFullWidth;}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.menu count]; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section == 0) {
return [self.section1 count];
} else if (section == 1) {
return [self.section2 count];
}}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0) {
return #"Contact Lens Brand";
} else if (section == 1) {
return #"Contact Lens Type";
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; }
if (indexPath.section == 0) {
cell.textLabel.text = [NSString stringWithFormat:#"%#", [self.section1 objectAtIndex:indexPath.row]];
} else if (indexPath.section == 1) {
cell.textLabel.text = [NSString stringWithFormat:#"%#", [self.section2 objectAtIndex:indexPath.row]];
}
return cell;}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UIViewController *newTopViewController;
if (indexPath.section == 0) {
if([[section1 objectAtIndex:indexPath.row] isEqual:#"Acuvue"]){
lens.lensInt= 0 ;
}
if([[section1 objectAtIndex:indexPath.row]isEqual:#"Bausch + Lomb"]){
lens.lensInt = 1 ;
}
NSString *identifier = [NSString stringWithFormat:#"product"];
newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
} else if (indexPath.section == 1) {
NSString *identifier = [NSString stringWithFormat:#"product"];
newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
}
[self.slidingViewController anchorTopViewOffScreenTo:ECRight animations:nil onComplete:^{
CGRect frame = self.slidingViewController.topViewController.view.frame;
self.slidingViewController.topViewController = newTopViewController;
self.slidingViewController.topViewController.view.frame = frame;
[self.slidingViewController resetTopView];}];
}
#end
you should pass the data between views using prepareForSegue if you are using storyboards.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];
// Pass object to your view controller here
[vc setMyObjectHere:object];
}
}

UITableView in UIViewController - Connecting cells to UIViewController

I have a UITableView in a UIViewController. I have other UIViewControllers created in Storyboard that I want to connect to my cells.
How can I connect UIViewControllers created in Storyboard to my cells?
MyViewController.h
#interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
IBOutlet UITableView *myTableView;
}
#property (nonatomic, retain) UITableView *myTableView;
MyViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *array = [[NSArray alloc] initWithObjects:#"CELL1", #"CELL2 & CELL3", nil];
self.listData = array;
[array release];
[myTableView setDataSource:self];
[myTableView setDelegate:self];
[[self view] addSubview:myTableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UpdatesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"UpdatesTableViewCell" owner:nil options:nil];
for (UIView *view in views) {
if([view isKindOfClass:[UITableViewCell class]])
{
cell = (UpdatesTableViewCell*)view;
}
}
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
}
You can either create segues from this controller to the other ones and then choose which segue to perform when someone taps a cell or you can use a reference to the storyboard to call instantiateViewControllerWithIdentifier: and make that controller active on a tap.

Form sheet with custom split view like UITableView crash when scrolling

I have created a iPad app that has a settings form sheet. The form sheet is using two subviews for create a split view like display. The left (master) UITableView scrolls and launches fine. The error is coming in from when one of the master cells is selected and it displays a UITableView from a UITableViewController into the right(detail) view. It will display the UITableView but once it is scrolled the detail view crashes. Yes I have the list pulling from plist because I want these results be be accessed later in other places of the application.
this is the View Controller for the form sheet and delegate for Master View
.h file
#interface settingsViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>{
UITableView *mainTableView;
NSDictionary *mainSettingList;
NSArray *settingItems;
}
#property (weak, nonatomic) IBOutlet UINavigationBar *settingsNavigationBar;
#property (weak, nonatomic) IBOutlet UITableView *mainTableView;
#property (weak, nonatomic) IBOutlet UIView *borderView;
#property (strong, nonatomic) IBOutlet UIView *detailView;
.m file
#property (nonatomic, strong)NSDictionary *mainSettingList;
#property (nonatomic, strong)NSArray *settingItems;
#end
#implementation settingsViewController
#synthesize settingsNavigationBar = _settingsNavigationBar;
#synthesize mainTableView = _mainTableView;
#synthesize borderView = _borderView;
#synthesize settingItems, mainSettingList;
-(void)viewDidLoad{
[super viewDidLoad];
[self.detailView setFrame:CGRectMake(233, 0, 310, 620)];
[self.borderView setBackgroundColor:[UIColor lightGrayColor]];
[self.view addSubview:self.borderView];
[self.view addSubview:self.detailView];
}
- (IBAction)closePressed:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
-(NSDictionary *)mainSettingList{
if(!mainSettingList){
NSString *str_settingList = [[NSBundle mainBundle]pathForResource:#"settingsList" ofType:#"plist"];
mainSettingList = [NSDictionary dictionaryWithContentsOfFile:str_settingList];
self.mainSettingList = mainSettingList;
}
return mainSettingList;
}
-(NSArray *)settingItems{
if(!settingItems){
settingItems = [[self.mainSettingList allKeys]sortedArrayUsingSelector:#selector(compare:)];
self.settingItems = settingItems;
}
return settingItems;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.settingItems.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *wordsInSection = [self.mainSettingList objectForKey:
[self.settingItems objectAtIndex:section]];
return wordsInSection.count;
}
-(NSString *)nameAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *wordsInSection = [self.mainSettingList objectForKey:[self.settingItems objectAtIndex:indexPath.section]];
return [wordsInSection objectAtIndex:indexPath.row];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"AlumniListCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.textLabel.text = [self nameAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [self.settingItems objectAtIndex:section];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UINavigationController *nvc = [[UINavigationController alloc]init];
[nvc.view setFrame:CGRectMake(0,0, 310, 620)];
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = selectedCell.textLabel.text;
if([cellText isEqualToString:#"Types of Jumps"]){
TypesOfJumps *toj = [[TypesOfJumps alloc]initWithNibName:#"TypesOfJumps" bundle:nil];
[nvc pushViewController:toj animated:YES];
}
[self.detailView addSubview:nvc.view];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:#"You selected %#!", cellText] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
return YES;
}
- (void)viewDidUnload {
[self setSettingsNavigationBar:nil];
[self setBorderView:nil];
[self setMainTableView:nil];
[self setSubViewNavigaionBar:nil];
[self setDetailView:nil];
[super viewDidUnload];
}
#end
UITableViewController called by cell pressed method
.h file
#interface TypesOfJumps : UITableViewController<UITableViewDataSource,UITableViewDelegate>{
NSDictionary *listJumps;
NSArray *typesJumps;
}
#property (strong, nonatomic) IBOutlet UITableView *tableView;
.m file
#interface TypesOfJumps ()
#property (strong, nonatomic)NSDictionary *listJumps;
#property (strong, nonatomic)NSArray *typesJumps;
#end
#implementation TypesOfJumps
#synthesize listJumps = _listJumps;
#synthesize typesJumps = _typesJumps;
#synthesize tableView = _tableView;
-(NSDictionary *)listJumps{
if(!listJumps){
NSString *str_settingList = [[NSBundle mainBundle]pathForResource:#"TypesOfJumps" ofType:#"plist"];
listJumps = [NSDictionary dictionaryWithContentsOfFile:str_settingList];
self.listJumps = listJumps;
}
return listJumps;
}
-(NSArray *)typesJumps{
if(!typesJumps){
typesJumps = [[self.listJumps allKeys]sortedArrayUsingSelector:#selector(compare:)];
self.typesJumps = typesJumps;
}
return typesJumps;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.typesJumps.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *wordsInSection = [self.listJumps objectForKey:
[self.typesJumps objectAtIndex:section]];
return wordsInSection.count;
}
-(NSString *)nameAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *wordsInSection = [self.listJumps objectForKey:[self.typesJumps objectAtIndex:indexPath.section]];
return [wordsInSection objectAtIndex:indexPath.row];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"AlumniListCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.textLabel.text = [self nameAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [self.typesJumps objectAtIndex:section];
}

Objective C: Label Text (subclass of UIControl) not displayed in UITableview

I am trying to display a UILabel text (subclass of UIControl) in a cell of a tableview controller.
My code as follows:
In UIControl label .h file
#import <Foundation/Foundation.h>
#interface UIControlLabel : UIControl
{
UILabel *userNameLabel;
}
#property (nonatomic, retain) UILabel *userNameLabel;
#end
In UIControl.m file
#import "UIControlLabel.h"
#implementation UIControlLabel
#synthesize userNameLabel;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
NSLog(#"in init with frame method");
self.userNameLabel = [[UILabel alloc] init];
[self addSubview: userNameLabel];
}
return self;
}
#end
In tableviewcontroller .m file
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* PlaceholderCellIdentifier = #"PlaceholderCell";
int row = [indexPath row];
Answer *thisAnswer = [self.array objectAtIndex:row];
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:PlaceholderCellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:PlaceholderCellIdentifier] autorelease];
UIControlLabel *control = [[UIControlLabel alloc]initWithFrame:CGRectMake(35, 10,100, 10)];
control.userNameLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:13.0];
control.tag = 2;
[cell.contentView addSubview:control];
}
UIControlLabel *thisControl = (UIControlLabel *)[cell.contentView viewWithTag:2];
thisControl.userNameLabel.text = [NSString stringWithFormat:#"%#",thisAnswer.userName];
return cell;
}
My issue is that the cell is not showing the label i set above. Is there something I am missing out here?
Seems like you're not setting a frame for your UILabel within your class.
Either call sizeToFit on UILabel, set the frame to match the whole size of your cell, use autosizeMask or implement -layoutSubviews in your UIControlLabel (then you might need to call [cell setNeedsLayout].