'-[UITableViewController loadView] loaded the "MySubscriptionsViewController" nib but didn't get a UITableView.' - iphone

I updated my xcode using 6.1 simulator, my app was working fine for 5.1 simulator,
now i am getting the following error :
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "MySubscriptionsViewController" nib but didn't get a UITableView.'
my header file:
#import <UIKit/UIKit.h>
#interface MySubscriptionsViewController : UITableViewController{
}
#property (nonatomic,retain) NSString *serverAddress;
#property (nonatomic, retain) UITextField *airportField;
#property (nonatomic, retain) NSMutableArray *airportList;
#property (nonatomic, retain) NSMutableArray *colCode;
#property (nonatomic, retain) NSMutableArray *colAirport;
#property (nonatomic, retain) NSMutableArray *colStartDate;
#property (nonatomic, retain) NSMutableArray *colStartTime;
#property (nonatomic, retain) NSMutableArray *colEndTime;
#property (nonatomic, retain) NSMutableDictionary *tempAirport;
#property (nonatomic,strong) NSDictionary *countryList;
#property (nonatomic,strong) NSArray *countryKeys;
- (IBAction)Meetup:(id)sender;
- (IBAction)ViewBeacon:(id)sender;
-(IBAction)gotoHome:(id)sender;
-( void)beacon:(NSString *)theStr;
#end
my m file:
#import "MySubscriptionsViewController.h"
#import "MybeaconsViewController.h"
#import "SBJsonParser.h"
#import "GridTableViewCell.h"
#import "MeetupViewController.h"
#import "ViewBeaconViewController.h"
#import "DataClass.h"
#import "MemberPanelViewController.h"
#interface MySubscriptionsViewController ()
#end
#implementation MySubscriptionsViewController
#synthesize countryList,countryKeys,serverAddress,airportField,airportList,tempAirport,colStartTime,colEndTime,colStartDate,colAirport,colCode;
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0,0);
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
DataClass *obj=[DataClass getInstance];
// serverAddress = #"http://www.cloudnetpk.com/transbeacon_design/services";
NSString *strURL = [NSString stringWithFormat:#"http://www.cloudnetpk.com/transbeacon_design/services/get_my_beacon_subscriptions.php?code=%#",obj.str];
NSLog(#" url => %#",strURL);
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSArray *datos = [parser objectWithString:strResult error:nil];
tempAirport = [[NSMutableDictionary alloc] init];
airportList = [[NSMutableArray alloc] init];
colStartTime = [[NSMutableArray alloc] init];
colEndTime = [[NSMutableArray alloc] init];
colStartDate= [[NSMutableArray alloc] init];
colAirport= [[NSMutableArray alloc] init];
colCode= [[NSMutableArray alloc] init];
countryList = datos;
NSLog(#"beacons count %d",datos.count);
for (int i=0; i<datos.count; i++){
int a=0;
NSString *startDate = [[[datos objectAtIndex:i] objectForKey:#"start_date" ] lowercaseString];
NSString *airPort= [[[datos objectAtIndex:i] objectForKey:#"airport" ] lowercaseString];
NSString *startTime = [[[datos objectAtIndex:i] objectForKey:#"start_time" ] lowercaseString];
NSString *endTime = [[[datos objectAtIndex:i] objectForKey:#"end_time" ] lowercaseString];
NSString *code = [[[datos objectAtIndex:i] objectForKey:#"code" ] lowercaseString];
//NSLog(#" here =>%#",code);
[colStartDate insertObject:startDate atIndex:i ];
[colAirport insertObject:airPort atIndex:i ];
[colStartTime insertObject:startTime atIndex:i ];
[colEndTime insertObject:endTime atIndex:i ];
[colCode insertObject:code atIndex:i];
//[ tempAirport setObject:lcString forKey:lcStringValue];
}
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [countryList count];
}
/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell =[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"cell"];
NSString *currentCountryName=[countryKeys objectAtIndex:[indexPath row]];
[[cell textLabel] setText:currentCountryName];
//cell.detailTextLabel.text=#"testing here ";
return cell;
}
*/
-(UITableViewCell *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
static NSString *CellIdentifier = #"SectionHeader";
//UITableViewCell *headerView = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(10,0,300,60)] ;
UIImage *myImage=[UIImage imageNamed:#"top_bar.png"];
UIImageView *imageView =[[UIImageView alloc] initWithImage:myImage];
imageView.frame= CGRectMake(0, 0, 400, 50);
[headerView addSubview:imageView];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(110,3, tableView.bounds.size.width - 170,40)];
label.text = #"Subscriber List";
label.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
label.font = [UIFont fontWithName:#"Arial-BoldMT" size:16];
label.backgroundColor = [UIColor clearColor];
[headerView addSubview:label];
UIButton *circularButton = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect circularRect = CGRectMake(5.0, 5, 58.0, 32.0);
[circularButton setFrame:circularRect];
[circularButton addTarget:self action:#selector(Meetup:) forControlEvents:UIControlEventTouchUpInside];
UIImage *buttonImage = [UIImage imageNamed:#"back_btn.png"];
[circularButton setImage:buttonImage forState:UIControlStateNormal];
[headerView addSubview:circularButton];
UIButton *homeButton = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect circularRectHome = CGRectMake(250.0, 5, 58.0, 32.0);
[homeButton setFrame:circularRectHome];
[homeButton addTarget:self action:#selector(gotoHome:) forControlEvents:UIControlEventTouchUpInside];
UIImage *buttonImageHome = [UIImage imageNamed:#"home_btn.png"];
[homeButton setImage:buttonImageHome forState:UIControlStateNormal];
[headerView addSubview:homeButton];
return headerView;
}
-(IBAction)gotoHome:(id)sender{
// redirect
MemberPanelViewController *window =[[MemberPanelViewController alloc]init];
[self presentModalViewController:window animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 50;
}
- (IBAction)Meetup:(id)sender{
NSLog(#" button clicked here");
MeetupViewController *window =[[MeetupViewController alloc]init];
[self presentModalViewController:window animated:YES];
}
- (IBAction)ViewBeacon:(id)sender{
ViewBeaconViewController *window=[[ViewBeaconViewController alloc]init];
[self presentModalViewController:window animated:YES];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
GridTableViewCell *cell = (GridTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[GridTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.lineColor = [UIColor blackColor];
}
// Since we are drawing the lines ourself, we need to know which cell is the top cell in the table so that
// we can draw the line on the top
if (indexPath.row == 0)
cell.topCell = YES;
else
cell.topCell = NO;
// Configure the cell.
//cell.cell1.text = [NSString stringWithFormat:#"%i",indexPath.row];
//cell.cell2.text = [NSString stringWithFormat:#"%i", indexPath.row];
//cell.cell3.text = #"test here text";
cell.cell1.text =[colStartDate objectAtIndex:indexPath.row];
cell.cell2.text =[colAirport objectAtIndex:indexPath.row];
cell.cell3.text =[colStartTime objectAtIndex:indexPath.row];
cell.cell4.text = [colEndTime objectAtIndex:indexPath.row];
NSString *value = [colCode objectAtIndex:indexPath.row];
NSLog(#" value => %#",value);
UIButton *circularButton = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect circularRect = CGRectMake(260.0, 2, 60.0, 40.0);
[circularButton setFrame:circularRect];
[circularButton addTarget:self action:#selector(beacon:) forControlEvents:UIControlEventTouchUpInside];
circularButton.tag=value;
UIImage *buttonImage = [UIImage imageNamed:#"view.png"];
[circularButton setImage:buttonImage forState:UIControlStateNormal];
[cell.contentView addSubview:circularButton];
return cell;
}
-( void)beacon:(NSString *)theStr{
NSInteger *tid = ((UIControl*)theStr).tag;
NSLog(#" here is parameter %#",tid);
ViewBeaconViewController *window=[[ViewBeaconViewController alloc]init];
// window.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
window.theDataYouWantToPass =tid;
window.lastscreen =#"subscription";
[self presentModalViewController:window animated:YES];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "MySubscriptionsViewController" nib but didn't get a UITableView.' This error Ocurce becouse some bellow issue.
I think the problem is because you are subclassing UITableViewController instead of UIViewController.
you need to put UIViewController as a subclass and connect IBoutlet of UITableview in XIB with its delegare. your TableVIew in UIView of XIB so you just connect fileOWner to UIView as a View and UITableview with your TableView Outlate.
#interface MySubscriptionsViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
OR
else there you remaining some referance of old copy you trying with restart xcode and remove catch or clean derive data

I just had the same issue; I fixed it by subclassing my newly created viewController file.
I was subclassing my newly created tableView file by mistake.
Or
If you want the table view to be the root view in your initial view (it will fill up the whole screen with cells) then you will delete the view in the project outline. second you will control drag the table view in the project outline to the menu (above it) and then you will click delegate for the outlet and do the same for the data source. finally you will open the table view controller file and add UITableViewDelegate and UITableViewDataSource so that it looks like this (using your created name of course).
class MainMenuTableViewController: UITableViewController, UITableViewDelegate, UITableViewDataSource
Hope this helps.

Related

Making UITextField from another class resign first responder

I have an app which has a UITableView that uses custom cells, which I created a new class for, CustomCell. A new cell is created every time a press a certain button, and every cell has a UITextField. I want to know how do I make the UITextField in a cell resign first responder after I tap it. Keep in mind that it is initialised in another class, called CustomCell.
UIViewController code:
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
}
-(void)dismissKeyboard{
}
(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier= #"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
if (indexPath.row%2==0){
n=1;
cell.inOut.text = [NSString stringWithFormat:#"Entrada %d:", indexPath.row/2 +1];
cell.entryTime.text = [NSString stringWithFormat:#"%#", [_entryArray objectAtIndex:(indexPath.row+2)]];
if (indexPath.row==0){
cell.delta.text=#"";
}
else {
cell.delta.text = [_timeSinceLastEntryArray objectAtIndex:indexPath.row];
}
}
else{
cell.inOut.text = [NSString stringWithFormat:#"SaĆ­da %d:", (indexPath.row+1)/2];
cell.entryTime.text = [NSString stringWithFormat:#"%#",[_entryArray objectAtIndex:(indexPath.row+2)]];
cell.delta.text = [_timeSinceLastEntryArray objectAtIndex:indexPath.row];
if (whichButton==YES){}
else{
}
n=2;
}
if( [indexPath row] % 2)
[cell setBackgroundColor:[UIColor whiteColor]];
else
[cell setBackgroundColor:[UIColor lightGrayColor]];
return cell;
}
CustomCell.h code:
#interface CustomCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UITextField *inOut;
#property (strong, nonatomic) IBOutlet UILabel *entryTime;
#property (strong, nonatomic) IBOutlet UILabel *delta;
- (void) dismissInOutKeyboard;
#end
CustomCell.m code:
#implementation CustomCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_inOut = [[UITextField alloc]init];
_entryTime = [[UILabel alloc]init];
_delta = [[UILabel alloc]init];
[self.contentView addSubview:_entryTime];
[self.contentView addSubview:_inOut];
[self.contentView addSubview:_delta];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#synthesize inOut=_inOut, entryTime=_entryTime, delta=_delta;
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {
// Initialization code
_inOut = [[UITextField alloc]init];
_inOut.textAlignment = UITextAlignmentLeft;
_entryTime = [[UILabel alloc]init];
_entryTime.textAlignment = UITextAlignmentLeft;
_delta = [[UILabel alloc]init];
_delta.textAlignment = UITextAlignmentLeft;
[self.contentView addSubview:_entryTime];
[self.contentView addSubview:_inOut];
[self.contentView addSubview:_delta];
}
return self;
}
#end
Simply call findAndResignFirstResponder for the top-most view you have , i.e. UIViewController.view
Declaration:
#interface UIView (findAndResignFirstResponder)
- (void)findAndResignFirstResponder;
- (UIView *)findFirstResponder;
#end
Definition:
#implementation UIView (findAndResignFirstResponder)
- (void)findAndResignFirstResponder {
[self.findFirstResponder resignFirstResponder];
}
- (UIView *)findFirstResponder {
for (UIView *subView in self.subviews) {
if (subView.isFirstResponder)
return subView;
UIView* firstResponder = subView.findFirstResponder;
if(firstResponder)
return firstResponder;
}
return nil;
}
One crude but working implementation that I have used is to create a new UITextField, set it to be a first responder, and then immediately delete it. A basic method of doing this (within a view) is to do something like this:
UITextField *cell = [UITextField new];
[self addSubview:cell];
[cell becomeFirstResponder];
[cell resignFirstResponder];
[cell removeFromSuperview];
cell = nil;
This creates a new cell, adds it to the view so that it can be selected, has it take the focus of the keyboard away from the currently selected cell, hides the keyboard, and then deletes the new cell.
Edit: If you are going to be setting another textfield to be the first responder afterwards, this code is entirely unnecessary. Then, you'd just have to call becomeFirstResponder on the new textfield, and the keyboard focus will automatically shift.

Xcode Error with Linking View Controllers with UITableView

I have made an a Tabbed Bar Application in Xcode and the First Tab is Decors. I asked a previous question on integrating a XIB Project into a StoryBoard Tabbed Project.
I have Been Successfully in the integration of this, And it works but when I push on one of the Deocrs, Or Table Cells I get the following Error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "SelectedCellViewController" nib but the view outlet was not set.
If I put the Original Alert into my project The cell linking starts to work again.
Below are pics, links and code to my project, So you can see how it works
StoryBoard Project
XIB Project
DecorsViewController_iPhone.h
#import <UIKit/UIKit.h>
#interface DecorsViewController_iPhone : UIViewController
{
IBOutlet UITableView *tableViewDecors;
NSArray *sitesArray;
NSArray *imagesArray;
}
#property (nonatomic, retain) UITableView *tableViewDecors;
#property (nonatomic, retain) NSArray *sitesArray;
#property (nonatomic, retain) NSArray *imagesArray;
#end
DecorsViewController_iPhone.m
#import "DecorsViewController_iPhone.h"
#import "SelectedCellViewController.h"
#interface DecorsViewController_iPhone ()
#end
#implementation DecorsViewController_iPhone
#synthesize tableViewDecors;
#synthesize sitesArray;
#synthesize imagesArray;
#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
// Load up the sitesArray with a dummy array : sites
NSArray *sites = [[NSArray alloc] initWithObjects:#"a", #"b", #"c", #"d", #"e", #"f", #"g", #"h", nil];
self.sitesArray = sites;
//[sites release];
UIImage *active = [UIImage imageNamed:#"a.png"];
UIImage *ae = [UIImage imageNamed:#"b.png"];
UIImage *audio = [UIImage imageNamed:#"c.png"];
UIImage *mobile = [UIImage imageNamed:#"d.png"];
UIImage *net = [UIImage imageNamed:#"e.png"];
UIImage *photo = [UIImage imageNamed:#"f.png"];
UIImage *psd = [UIImage imageNamed:#"g.png"];
UIImage *vector = [UIImage imageNamed:#"h.png"];
NSArray *images = [[NSArray alloc] initWithObjects: active, ae, audio, mobile, net, photo, psd, vector, nil];
self.imagesArray = images;
//[images release];
[super viewDidLoad];
}
#pragma mark - Table View datasource methods
// Required Methods
// Return the number of rows in a section
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
return [sitesArray count];
}
// Returns cell to render for each row
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure cell
NSUInteger row = [indexPath row];
// Sets the text for the cell
//cell.textLabel.text = [sitesArray objectAtIndex:row];
// Sets the imageview for the cell
cell.imageView.image = [imagesArray objectAtIndex:row];
// Sets the accessory for the cell
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Sets the detailtext for the cell (subtitle)
//cell.detailTextLabel.text = [NSString stringWithFormat:#"This is row: %i", row + 1];
return cell;
}
// Optional
// Returns the number of section in a table view
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
#pragma mark -
#pragma mark Table View delegate methods
// Return the height for each cell
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 78;
}
// Sets the title for header in the tableview
-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return #"Decors";
}
// Sets the title for footer
-(NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
return #"Decors";
}
// Sets the indentation for rows
-(NSInteger) tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {
return 0;
}
// Method that gets called from the "Done" button (From the #selector in the line - [viewControllerToShow.navigationItem setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissView)] autorelease]];)
- (void)dismissView {
[self dismissViewControllerAnimated:YES completion:NULL];
}
// This method is run when the user taps the row in the tableview
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
SelectedCellViewController *viewControllerToShow = [[SelectedCellViewController alloc] initWithNibName:#"SelectedCellViewController" bundle:[NSBundle mainBundle]];
[viewControllerToShow setLabelText:[NSString stringWithFormat:#"You selected cell: %d - %#", indexPath.row, [sitesArray objectAtIndex:indexPath.row]]];
[viewControllerToShow setImage:(UIImage *)[imagesArray objectAtIndex:indexPath.row]];
[viewControllerToShow setModalPresentationStyle:UIModalPresentationFormSheet];
[viewControllerToShow setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[viewControllerToShow.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissView)]];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewControllerToShow];
viewControllerToShow = nil;
[self presentViewController:navController animated:YES completion:NULL];
navController = nil;
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Tapped row!"
// message:[NSString stringWithFormat:#"You tapped: %#", [sitesArray objectAtIndex:indexPath.row]]
// delegate:nil
// cancelButtonTitle:#"Yes, I did!"
// otherButtonTitles:nil];
// [alert show];
// [alert release];
}
#pragma mark - Memory management
- (void)didReceiveMemoryWarning {
NSLog(#"Memory Warning!");
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
self.sitesArray = nil;
self.imagesArray = nil;
[super viewDidUnload];
}
//- (void)dealloc {
//[sitesArray release];
//[imagesArray release];
// [super dealloc];
//}
#end
SelectedCellViewController.h
#interface SelectedCellViewController : UIViewController {
NSString *labelText;
UIImage *image;
IBOutlet UILabel *label;
IBOutlet UIImageView *imageView;
}
#property (nonatomic, copy) NSString *labelText;
#property (nonatomic, retain) UIImage *image;
#end
viewControllerToShow.m
#import "SelectedCellViewController.h"
#implementation SelectedCellViewController
#synthesize labelText;
#synthesize image;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
}
return self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
[label setText:self.labelText];
[imageView setImage:self.image];
}
- (void)viewDidUnload {
self.labelText = nil;
self.image = nil;
//[label release];
// [imageView release];
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
check the class inherited by the "The controller to be shown on click".XIB in the identity inspector .It seems you have not set the class for the "to be shown" XIB and the view outlet of the same XIB.

TableView with 2 colums by row , iphone/ipad

Can anybody tell me how to create a tableview
with two cells by colums in the Iphone SDK??
order in like this :
_____________
| 1 | 2 |
|_____|______|
| 3 | 4 |
|_____|______|
Best regards
In ios UITableView you can only have one column/cell. If you want more propertyes, in a same cell you need to create a custom table view cell by subcalssing UITableViewCell. In interface builder you can customize the layout. TUTORIAL: http://adeem.me/blog/2009/05/30/iphone-sdk-tutorial-part-6-creating-custom-uitableviewcell-using-interface-builder-uitableview/
Concrete example for your problem:
the .h file:
#import <UIKit/UIKit.h>
#interface MyCell : UITableViewCell {
UIButton * column1;
UIButton * column2;
UILabel* column1_label1;
UILabel* column1_label2;
UILabel* column2_label1;
UILabel* column2_label2;
}
#property (nonatomic,retain) UIButton * column1;
#property (nonatomic,retain) UIButton * column2;
#property (nonatomic,retain) UILabel* column1_label1;
#property (nonatomic,retain) UILabel* column1_label2;
#property (nonatomic,retain) UILabel* column2_label1;
#property (nonatomic,retain) UILabel* column2_label2;
#end
the .m file:
#import "MyCell.h"
#implementation MyCell
#synthesize column1,column1_label1,column1_label2,column2,column2_label1,column2_label2;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
column1 = [UIButton buttonWithType: UIButtonTypeCustom];
column1.tag = 1;
column1.autoresizingMask = 292;
column1.backgroundColor = [UIColor redColor];
column1.frame = CGRectMake(0, 0, self.contentView.bounds.size.width/2, self.contentView.bounds.size.height);
column1_label1 = [[UILabel alloc] initWithFrame: CGRectMake(column1.bounds.size.width*1/5,
0,
column1.bounds.size.width*4/5,
column1.bounds.size.height/2)];
column1_label1.backgroundColor = [UIColor redColor];
[column1 addSubview:column1_label1];
column1_label2 = [[UILabel alloc] initWithFrame: CGRectMake(column1.bounds.size.width*1/5,
column1.bounds.size.height/2,
column1.bounds.size.width*4/5,
column1.bounds.size.height/2)];
column1_label2.backgroundColor = [UIColor greenColor];
[column1 addSubview:column1_label2];
[self.contentView addSubview: column1];
column2 = [UIButton buttonWithType: UIButtonTypeCustom];
column2.tag = 2;
column2.autoresizingMask = 292;
column2.backgroundColor = [UIColor greenColor];
column2.frame = CGRectMake(self.contentView.bounds.size.width/2, 0, self.contentView.bounds.size.width/2, self.contentView.bounds.size.height);
column2_label1 = [[UILabel alloc] initWithFrame: CGRectMake(column2.bounds.size.width*1/5,
0,
column2.bounds.size.width*4/5,
column2.bounds.size.height/2)];
column2_label1.backgroundColor = [UIColor redColor];
[column2 addSubview:column2_label1];
column2_label2 = [[UILabel alloc] initWithFrame: CGRectMake(column2.bounds.size.width*1/5,
column2.bounds.size.height/2,
column2.bounds.size.width*4/5,
column2.bounds.size.height/2)];
column2_label2.backgroundColor = [UIColor greenColor];
[column2 addSubview:column2_label2];
[self.contentView addSubview: column2];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)dealloc
{
[super dealloc];
}
#end
the usage in UITableViewController:
- (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 40;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
MyCell * mycell2 = ((MyCell*)cell);
if (cell == nil) {
cell = [[[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
[mycell2.column1 addTarget:self action:#selector(column1Selected:) forControlEvents:UIControlEventTouchUpInside];
[mycell2.column2 addTarget:self action:#selector(column1Selected:) forControlEvents:UIControlEventTouchUpInside];
}
// Configure the cell...
mycell2.column1_label1.text = #"column1_label1";
mycell2.column1_label2.text = #"column1_label2";
mycell2.column1.tag = indexPath.row*2;
mycell2.column2_label1.text = #"column2_label1";
mycell2.column2_label2.text = #"column2_label2";
mycell2.column2.tag = indexPath.row*2 +1;
return cell;
}
- (void) column1Selected: (id) sender
{
UIAlertView *alert = [[ UIAlertView alloc]
initWithTitle: #" Alert"
message: [NSString stringWithFormat: #"button %d",((UIButton *) sender).tag]
delegate: nil
cancelButtonTitle: #" OK"
otherButtonTitles: nil];
[alert show] ;
[alert release];
}
In iOS6 you can use the UICollectionView.
If you need iOS5 support AQGridView is a grid control that will give you what you are after.

Delegate methods of UITableView are not responding

I am quite new to IOS development, so forgive me if this problem turns out to be trivial. The following is the case:
I want to be able to call the didSelectRowAtIndexPath method to trigger some action when a user interacts with a table view cell. However, it seems that this method is never called for some reason. I am fairly certain that my code is valid. I set the delegate and dataSource properties and I conform to the UITableViewDelegate and UITableViewDataSource protocols.
If someone could shed some light on this, I would be very grateful.
My code:
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#class Employee;
#define kTelephoneRowIndex 0
#define kEmailRowIndex 1
#define kLeftLabelTag 4096
#define kRightLabelTag 4097
#interface PersonnelDetailsViewController : UIViewController <UITableViewDelegate,
UITableViewDataSource, MFMailComposeViewControllerDelegate> {
UITableView *contactDetailsTable;
Employee *employee;
}
#property (nonatomic, retain) UITableView *contactDetailsTable;
#property (nonatomic, retain) Employee *employee;
+ (NSString *)resourceFilePath:(NSString *)fileName ofType:(NSString *)type;
#end
and my .m file
#import <QuartzCore/QuartzCore.h>
#import "PersonnelDetailsViewController.h"
#import "Employee.h"
#implementation PersonnelDetailsViewController
#synthesize contactDetailsTable, employee;
+ (NSString *)resourceFilePath:(NSString *)fileName ofType:(NSString *)type {
return [[NSBundle mainBundle] pathForResource:fileName ofType:type];
}
- (void)loadView {
[super loadView];
// Create the header view for the table view
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 110)];
// Set the image, using rounded corners
UIImage *employeeImage;
if ([employee.image length] == 0)
employeeImage = [UIImage imageNamed:#"logoL1nda70by80"];
else
employeeImage = [UIImage imageNamed:employee.image];
UIImageView *employeeImageView = [[UIImageView alloc] initWithImage:employeeImage];
employeeImageView.frame = CGRectMake(10, 15, 70, 80);
employeeImageView.layer.masksToBounds = YES;
employeeImageView.layer.cornerRadius = 5.0;
employeeImageView.layer.borderWidth = 1.0;
employeeImageView.layer.borderColor = [[UIColor grayColor] CGColor];
[headerView addSubview:employeeImageView];
UILabel *nameView = [[UILabel alloc] initWithFrame:CGRectMake(95, 35, 180, 20)];
nameView.text = employee.name;
nameView.font = [UIFont boldSystemFontOfSize:18];
[headerView addSubview:nameView];
UILabel *functionView = [[UILabel alloc] initWithFrame:CGRectMake(95, 55, 140, 16)];
functionView.text = employee.function;
functionView.textColor = [UIColor grayColor];
functionView.font = [UIFont systemFontOfSize:14];
[headerView addSubview:functionView];
[employeeImage release];
[employeeImageView release];
[nameView release];
[functionView release];
contactDetailsTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 370) style: UITableViewStyleGrouped];
contactDetailsTable.backgroundColor = [UIColor clearColor];
contactDetailsTable.tableHeaderView = headerView;
[headerView release];
contactDetailsTable.delegate = self;
contactDetailsTable.dataSource = self;
[self.view addSubview:contactDetailsTable];
}
- (void)viewDidUnload {
self.contactDetailsTable = nil;
self.employee = nil;
[super viewDidUnload];
}
- (void)viewWillAppear:(BOOL)animated {
[contactDetailsTable reloadData];
}
- (void)dealloc {
[contactDetailsTable release];
[employee release];
[super dealloc];
}
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;did
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *contactDetailIdentifier = #"contactDetailIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:contactDetailIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:contactDetailIdentifier] autorelease];
UILabel *leftLabel = [[UITextField alloc] initWithFrame:CGRectMake(5, 12, 70, 25)];
leftLabel.textAlignment = UITextAlignmentRight;
leftLabel.textColor = [UIColor grayColor];
leftLabel.tag = kLeftLabelTag;
leftLabel.userInteractionEnabled = NO;
[cell.contentView addSubview:leftLabel];
[leftLabel release];
UILabel *rightLabel = [[UITextField alloc] initWithFrame:CGRectMake(90, 12, 160, 25)];
rightLabel.font = [UIFont boldSystemFontOfSize:16];
rightLabel.tag = kRightLabelTag;
rightLabel.userInteractionEnabled = NO;
[cell.contentView addSubview:rightLabel];
[rightLabel release];
}
NSUInteger row = [indexPath row];
UILabel *leftLabel = (UILabel *)[cell viewWithTag:kLeftLabelTag];
UILabel *rightLabel = (UILabel *)[cell viewWithTag:kRightLabelTag];
switch (row) {
case kTelephoneRowIndex:
leftLabel.text = #"telefoon";
rightLabel.text = employee.phone;
break;
case kEmailRowIndex:
leftLabel.text = #"e-mail";
rightLabel.text = employee.email;
break;
default:
break;
}
return cell;
}
#pragma mark -
#pragma mark Table View Delegate Methods
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = indexPath.row;
if (row == 1) {
MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
[mailController setToRecipients:[NSArray arrayWithObject:employee.email]];
[mailController.navigationBar setTintColor:[UIColor blackColor]];
[self presentModalViewController:mailController animated:YES];
mailController.mailComposeDelegate = self;
[mailController release];
}
NSLog(#"Row selected is: %d", row);
}
#pragma mark -
#pragma mark MFMailCompose View Delegate Methods
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
[self dismissModalViewControllerAnimated:YES];
}
#end
Check the doc for tableView:willSelectRowAtIndexPath::
Return an NSIndexPath object other than indexPath if you want another cell to be selected. Return nil if you don't want the row selected.
You are returning nil, basically saying the table view that you don't want that row selected. Therefore tableView:didSelectRowAtIndexPath: is not called.
When I had this problem I found the solution was to use willSelectRowAtIndexPath. In other places in the same project didSelectItemAtIndexPath is getting called fine.
#pragma mark - UITableViewDelegate
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// for some reason this is getting called instead of didSelectItemAtIndexPath
[self viewDetails:indexPath.row];
return indexPath;
}
- (void)tableView:(UITableView *)tableView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
// [self viewDetails:indexPath.row];
}

EXC_BAD_ACCESS on UITableView reloadData call

I've come to the point of pulling my hair out over this one. I keep getting a EXC_BAD_ACCESS when I call the reloadData for the UITableView. I'll lay the groundwork for you here.
MainViewController.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#class iPROSAppDelegate;
#interface MainViewController : UIViewController <UIScrollViewDelegate, UITableViewDelegate, UITableViewDataSource> {
iPROSAppDelegate *parent;
...
NSMutableArray *contentSeqList;
UITableView *contentSeqTable;
}
#property (nonatomic, retain) IBOutlet iPROSAppDelegate *parent;
...
#property (nonatomic, retain) NSMutableArray *contentSeqList;
#property (nonatomic, retain) IBOutlet UITableView *contentSeqTable;
...
- (IBAction)changeMainView:(id)sender;
#end
MainViewController.m
- (IBAction)changeMainView:(id)sender{
//do logs to make sure we get here and grab an NSDictionary object from another class
NSLog(#"got to a change of main view");
NSDictionary *myContentSequences = [[parent csList] grabCSListandReturnJSON:[sender tag]];
NSLog(#"got cs list for tag %#: %#", [sender currentTitle], myContentSequences);
//set up table view
contentSeqTable = [[UITableView alloc] initWithFrame:CGRectMake(40, 20, 240, 300) style:UITableViewStyleGrouped];
contentSeqTable.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
[contentSeqTable setDelegate:self];
[contentSeqTable setDataSource:self];
NSLog(#"delegate: %#, data source: %#", [contentSeqTable delegate], [contentSeqTable dataSource]);
contentSeqList = [[NSMutableArray alloc] init];
//populate nsmutablearray contentseqlist using the nsdictionary mycontentsequences
for (id dashboard in [myContentSequences objectForKey:#"dashboards"]){
if ([dashboard objectForKey:#"error"] == nil){
for (id contentsequence in [dashboard objectForKey:#"contentSequences"]){
[contentSeqList addObject:[contentsequence objectForKey:#"name"]];
NSLog(#"added object to content sequence list: %#", [contentsequence objectForKey:#"name"]);
}
}
}
[contentSeqTable reloadData];
...
}
changeMainView is called when a button is clicked in the main view. Here are the delegate methods also for UITableView. Notice that the NSLogs are the first lines in these methods, but I get neither showing in the console log before the EXC_BAD_ACCESS.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSLog(#"content seq list count: %#", [contentSeqList count]);
return [contentSeqList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(#"setting up the cells");
NSUInteger index = [indexPath indexAtPosition:1];
NSString *myId = #"id";
if (tableView.editing) myId = #"idE";
UILabel *name;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myId];
if (cell == nil){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 20) reuseIdentifier:myId] autorelease];
float width = 260;
if (tableView.editing) width = 240;
name = [[[UILabel alloc] initWithFrame:CGRectMake(45, 1, width, 53)] autorelease];
[name setBackgroundColor:[UIColor clearColor]];
[name setTextColor:[UIColor blackColor]];
[name setFont:[UIFont fontWithName:#"Helvetica" size: 18]];
[name setAdjustsFontSizeToFitWidth:YES];
[name setMinimumFontSize:11];
[name setNumberOfLines:2];
name.tag = 1;
[cell.contentView addSubview:name];
}
else{
name = (UILabel *)[cell.contentView viewWithTag:1];
}
if ([tableView indexPathForSelectedRow]){
if (indexPath == [tableView indexPathForSelectedRow]){
[name setTextColor:[UIColor whiteColor]];
}
else{
[name setTextColor:[UIColor blackColor]];
}
}
else{
[name setTextColor:[UIColor blackColor]];
}
[name setText:[contentSeqList objectAtIndex:index]];
return cell;
}
The delegate methods are part of the same class, MainViewController. I've also checked that the UITableView was not nil via NSLog right before reloadData. I know I have to be missing something small here. Any suggestions would be much appreciated. Thanks!
Can you show call stack when EXC_BAD_ACCESS occurs?
BTW,
NSUInteger index = [indexPath row];
is much more readable than
NSUInteger index = [indexPath indexAtPosition:1];