Ive been trying to push a new view when a cell is tapped but absolutely nothing happens. I figured grouped style pushed the same as plain. Here is my code:
-(void)viewDidLoad {
[super viewDidLoad];
contactArray = [[NSArray alloc] initWithObjects:#"iPhone",#"iPod",#"MacBook",#"MacBook Pro",nil];
shareArray = [[NSArray alloc] initWithObjects:#"Flex",#"AIR",#"PhotoShop",#"Flash",nil];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
// Customize the number of rows in the table view.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(section == 0)
return [contactArray count];
else
return [shareArray count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if(section == 0){
return #"Contact";
}else{
return #"Share";
}
}
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
if(section == 0){
return #"Footer for Apple Products";
}else{
return #"Footer for Adobe Softwares";
}
}
// Customize the appearance of table view cells.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
if(indexPath.section == 0){
cell.text = [contactArray objectAtIndex:indexPath.row];
}else{
cell.text = [shareArray objectAtIndex:indexPath.row];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//NextViewController *nextController = [[NextViewController alloc] initWithNibName:#"NextView" bundle:nil];
//[self.navigationController pushViewController:nextController animated:YES];
if([contactArray objectAtIndex:indexPath.row] == #"iPhone"){
LandscapeHydrogen *abo = [[LandscapeHydrogen alloc] initWithNibName:#"LandscapeHydrogen" bundle:nil];
[self.navigationController pushViewController:abo animated:NO];
[abo release];
}
}
Any help is appreciated.
You could put a breakpoint at the didSelectRowAtIndexPath to see if the if statement is true.
Try changing
if([contactArray objectAtIndex:indexPath.row] == #"iPhone"){
to
if([[contactArray objectAtIndex:indexPath.row] isEqualToString:#"iPhone"]){
YourNextViewController * nextViewController = [[YourNextViewController alloc] initWithNibName: #"YourNextViewController" bundle: nil];
switch (indexPath.section) {
case 0:
nextViewController.title = [_contactArray objectAtIndex: [indexPath row]];
break;
case 1:
nextViewController.title = [_shareArray objectAtIndex: [indexPath row]];
break;
default:
break;
}
[[self navigationController] pushViewController:nextViewController animated:YES];
}
Solved with
else if (indexPath.section == 1 & indexPath.row == 0){
FaceBook *nextController = [[FaceBook alloc] initWithNibName:#"FaceBook" bundle:nil];
[self.navigationController pushViewController:nextController animated:YES];
Related
I want to show data from two arrays, topScoreArray on first section and dataArray on second section of tableview. My code for dividing tableview into two sections is working well but it always shows only dataArray on both sections. I don't know what mistake I'm making.
- (void)viewDidLoad
{
[super viewDidLoad];
gameSoundsView.hidden = YES;
// Do any additional setup after loading the view from its nib.
dataArray = [[NSMutableArray alloc] initWithObjects: #"Terms and conditions",#"Contact us",#"Like us on facebook",nil];
topScoreArray = [[NSMutableArray alloc] init];
topScoreArray = [[NSMutableArray alloc] initWithObjects:#"Top Scores",#"Current Score",nil];
}
and my tableview is like this
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 57;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
{
return topScoreArray.count;
}
if (section == 1)
{
return dataArray.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"Custom CellCC";
customCellCC *cell = (customCellCC *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"customCellCC" owner:self options:nil];
cell = (customCellCC *)[nib objectAtIndex:0];
}
if (indexPath.row %2 ==0) {
UIView *view =[[UIView alloc]initWithFrame:cell.frame];
view.backgroundColor=[UIColor greenColor];
cell.backgroundView =view;
cell.textLabel.backgroundColor=[UIColor clearColor];
}
else
{
UIView *view =[[UIView alloc]initWithFrame:cell.frame];
view.backgroundColor=[UIColor colorWithRed:(255.0/255.0) green:(185.0/255.0) blue:(15.0/255.0) alpha:1.0f];
cell.backgroundView =view;
cell.textLabel.backgroundColor=[UIColor clearColor];
}
if (indexPath.section == 0) {
cell.lbltxt.text = [topScoreArray objectAtIndex:indexPath.row];
}
if (indexPath.section == 1)
{
cell.lbltxt.text = [dataArray objectAtIndex:indexPath.row];
}
return cell;
}
I am opening a .xib file which contains one UITableView in my UIPopViewController and I have given datasource and delegate methods of UIATableView.
But after calling cellForRowAtIndexPath method my app get crashed.
My code is as follows:
- (IBAction)btnOilChangePressed:(id)sender {
OilChangeView *oilVC = [[OilChangeView alloc] initWithNibName:#"OilChangeView" bundle:nil];
UIPopoverController *popView = [[UIPopoverController alloc] initWithContentViewController:oilVC];
[popView setPopoverContentSize:CGSizeMake(480, 250)];
[popView presentPopoverFromRect:btnOilChange.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}
following is the code on OilChangeView.m file
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arrayInfo count];
}
- (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];
}
cell.textLabel.text = [arrayInfo objectAtIndex:indexPath.row];
NSLog(#"text:%#",[arrayInfo objectAtIndex:indexPath.row]);
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Configure the cell...
return cell;
}
I am not able to add a new row in a UITableview,the code is as follows,
it showing an error with
-(void)viewDidLoad
{
self.title=#"CHECK-list";
self.navigationItem.rightBarButtonItem = self.editButtonItem;
addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addItem)];
self.navigationItem.leftBarButtonItem = addButton;
ar=[[NSMutableArray alloc]initWithObjects:#"Map",#"Camera",#"First Aid Kit", nil];
[super viewDidLoad];
}
- (void)addItem
{
//NSMutableArray *tempArray = [[NSMutableArray alloc] init];
int i = 0;
for (NSArray *count in ar)
{
[ar addObject:[NSIndexPath indexPathForRow:i++ inSection:1]];
}
[[self tableView] beginUpdates];
[[self tableView] insertRowsAtIndexPaths:(NSArray *)ar withRowAnimation:UITableViewRowAnimationNone];
[[self tableView] endUpdates];
[self.tableView reloadData];
}
Here Fazil.... basic code for any tableview and the row addition
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
UITableView *tableview = [[UITableView alloc]initWithFrame:CGRectMake(160, 280, 150, 150) style:UITableViewStylePlain];
tableview.delegate = self;
tableview.dataSource = self;
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier ];
}
cell.textLabel.text= [array objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[imagearray objectAtIndex:indexPath.row]];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cell_normal.png"]];
cell.textLabel.textColor = [UIColor blackColor];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:17.0];
return cell;
}
#pragma mark UItableView delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
check = indexPath.row;
UITableViewCell* theCell = [tableView cellForRowAtIndexPath:indexPath];
theCell.contentView.backgroundColor=[UIColor clearColor];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath
{
[tableview moveRowAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForRow:[array count] - 1 inSection:1]];
}
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
if (sourceIndexPath.section != proposedDestinationIndexPath.section) {
NSInteger row = 0;
if (sourceIndexPath.section < proposedDestinationIndexPath.section) {
row = [tableView numberOfRowsInSection:sourceIndexPath.section] - 1;
}
return [NSIndexPath indexPathForRow:row inSection:sourceIndexPath.section];
}
return proposedDestinationIndexPath;
}
I have been programming a UITableView and each cells pushes a new view, All I Want to do is add two new sections one male and one female, first and second voice need to be in the male section and the third voice needs to be in the female section.
#import "FirstLevelViewController.h"
#import "SecondLevelViewController.h"
#import "DisclosureDetailController.h"
#import "SecondVoiceController.h"
#import "ThirdVoiceController.h"
#implementation FirstLevelViewController
#synthesize controllers;
-(void)viewDidLoad {
self.title = #"Voices";
NSMutableArray *male = [[NSMutableArray alloc] init];
DisclosureDetailController *th = [DisclosureDetailController alloc];
th.title = #"First Voice";
[male addObject:th];
[th release];
SecondVoiceController *array2 = [SecondVoiceController alloc];
array2.title = #"Second Voice";
[male addObject:array2];
[array2 release];
ThirdVoiceController *array3 = [ThirdVoiceController alloc];
array3.title = #"Third Voice";
[male addObject:array3];
[array3 release];
self.controllers = male;
[male release];
[super viewDidLoad];
}
-(void)viewDidUnload {
self.controllers = nil;
[super viewDidUnload];
}
-(void)dealloc {
[controllers release];
[super dealloc];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.controllers count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *FirstLevelCell= #"FirstLevelCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstLevelCell];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstLevelCell] autorelease];
}
NSUInteger row = [indexPath row];
SecondLevelViewController *controller = [controllers objectAtIndex:row];
cell.textLabel.text = controller.title;
cell.imageView.image = controller.rowImage;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
SecondLevelViewController *nextViewController = [self.controllers
objectAtIndex:row];
[self.navigationController pushViewController:nextViewController animated:YES];
}
All I Want to do is add two new sections one male and one female, first and second voice need to be in the male section and the third voice needs to be in the female section. Please help been stuck on this for a while!
The tableView delegate has a method called numberOfSectionsInTableView. Return the number of sections that you want to create.
Then, in the cellForRowAtIndexPath use indexPath's other property [indexPath section] to segregate rows based on sections.
An example
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2; //one male and other female
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch(section){
case 0:
return [male count];
break;
case 1:
return [female count];
break;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *FirstLevelCell= #"FirstLevelCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstLevelCell];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstLevelCell] autorelease];
}
SecondLevelViewController *controller;
switch([indexPath section]){
case 0:
controller = [male objectAtIndex: [indexPath row] ];
break;
case 1:
controller = [female objectAtIndex: [indexPath row] ];
break;
}
cell.textLabel.text = controller.title;
cell.imageView.image = controller.rowImage;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
Hows this?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
switch (section) {
case 0:
return # of rows in section;
break;
case 1:
return # of rows in section;
break;
}
return 0;
}
I am having some issues with pulling up specific ViewControllers depending on which row is selected in my UITableView. I have a table, which contains 3 sections, with 4 rows in each section.
When a row is selected I have it set to call a new view for each row, so 12 views in total to be called depending on which row is selected. The issue is, that only the first 4 views are called, when I tap row number 5, it displays a new view, but it pulls up the first view.
I have set my UITableView up using the following code;
carbData = [[NSArray alloc] initWithObjects:#"What Are Carbs?",#"Why Do I Need Carbs?",#"Simple Vs. Complex",#"Fun Five Facts",nil];
proteinData = [[NSArray alloc] initWithObjects:#"What is Protein?",#"Why Do I Need Protein?",#"Complete Vs. Incomplete",#"Fun Five Facts",nil];
fatData = [[NSArray alloc] initWithObjects:#"What Are Fats?",#"Why Do I Need Fats?",#"Saturated Vs. Unsaturated",#"Fun Five Facts",nil];
[self setTitle:#"Nutrition Table"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.carbData count];
return [self.proteinData count];
return [self.fatData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SimpleTableIdentifier] autorelease];
}
if(indexPath.section == 0)
cell.text = [carbData objectAtIndex:indexPath.row];
if(indexPath.section == 1)
cell.text = [proteinData objectAtIndex:indexPath.row];
if (indexPath.section == 2)
cell.text = [fatData objectAtIndex:indexPath.row];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if(section == 0)
return #"Carbohydrates";
if (section ==1)
return #"Protein";
else if (section ==2)
return #"Fats";
}
I then use the following code to determine which view is to be called, when a certain row is selected;
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([[carbData objectAtIndex:indexPath.row] isEqual:#"What Are Carbs?"]) {
CarbsView1 *controller = [[CarbsView1 alloc] initWithNibName:#"CarbsView1" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
if ([[carbData objectAtIndex:indexPath.row] isEqual:#"Why Do I Need Carbs?"]) {
CarbsView2 *controller = [[CarbsView2 alloc] initWithNibName:#"CarbsView2" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
if ([[carbData objectAtIndex:indexPath.row] isEqual:#"Simple Vs. Complex"]) {
CarbsView3 *controller = [[CarbsView3 alloc] initWithNibName:#"CarbsView3" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
if ([[carbData objectAtIndex:indexPath.row] isEqual:#"Fun Five Facts"]) {
CarbsView4 *controller = [[CarbsView4 alloc] initWithNibName:#"CarbsView4" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
if ([[proteinData objectAtIndex:indexPath.row] isEqual:#"What is Protein?"]) {
ProteinView1 *controller = [[CarbsView4 alloc] initWithNibName:#"ProteinView1" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
The app compiles and builds fine, everything works as i'd like it too, however when looking at the titles used, when I try and select "What is Protein?" is pulls up the view for "what are Carbs?" which is in the first if statement.
Any help would be massively appreciated
"What is Protein?" is in section 1, row 0. "What are Carbs?" is in section 0, row 0. Notice that your didSelectRowAtIndexPath method, you don't check the section number anywhere. So when you tap section 1, row 0, your first if statement returns true. I think you want something more like:
if (indexPath.section == 0) {
if(indexPath.row == 0) {
//present appropriate view controller
} else if (indexPath.row == 1) {
} else if (indexPath.row == 2) {
} ...
} else if (indexPath.section == 1) {
if (indexPath.row == 0) {
} else if (indexPath.row == 1) {
} ...
} ...