My tableViewcell is in array and i want to select diary cell from which i need to navigate to next screen.i dont know what to implement in did select row method.please help me out
table=[[UITableView alloc]init];
table.frame=CGRectMake(0, 0, 250, CGRectGetHeight(self.view.frame));
table.delegate=self;
table.dataSource=self;
table.backgroundColor=RGBCOLOR(255,255,255);
tabledata=[NSMutableArray arrayWithObjects:#"Me and My stuff",#"Upgrade Membership",#"Diary",#"Progress",#"Friends",#"My awards and points",#"Messages",#"Blogs",#"Fun stuff",#"Remainders",#"Settings",#"Signout",nil];
table.backgroundColor=RGBCOLOR(255,239,213);
table.sectionFooterHeight=44;
table.sectionHeaderHeight=44;
table.separatorStyle=UITableViewCellSeparatorStyleNone;
[self.view addSubview:table];
self.view.backgroundColor=RGBCOLOR(255,239,213);
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellidentifier =#"table";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellidentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier];
cell.opaque=NO;
cell.backgroundColor=RGBCOLOR(255,239,213);
cell.textLabel.textColor=RGBCOLOR(32,178,170);
cell.selectionStyle=UITableViewCellSelectionStyleBlue;
}
cell.textLabel.text=[tabledata objectAtIndex:indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
assume that the tableview contains in our first view controller
1. SecondViewController.h
#property(nonatomic, strong) NSString *fullName;
2. SecondViewController.m
#synthisize fullName;
- (void)viewDidLoad
{
NSLog(#"name ==%#",fullName);
[super viewDidLoad];
}
3. FirstViewController.m
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row==2) // this is diary cell
{
SecondViewController *temp = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
temp.fullName=[tabledata objectAtIndex:indexPath.row];
[self.navigationController pushViewController:temp animated:YES];
}
else
{
// do nothing
}
}
Related
I have a tableview called AdminOrderViewController and it has customcell called StepperProgressCell.
This customcell has a custom UIView called AYStepperView. There is a button in this UIView and I implemented a delegate on it, whenever it gets clicked and I clicked method is getting called on AdminOrderViewController.
However, I could not able to figure out how to pass clicked header cell.section to AYStepperView ??
AdminOrderViewController.m
#interface AdminOrderViewController : UIViewController <AYStepperViewDelegate>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"StepperProgressCell";
StepperProgressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[StepperProgressTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.stepperView.delegate= self;
return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
AdminHeaderFooterView *sectionHeaderView = [self.adminTableView dequeueReusableHeaderFooterViewWithIdentifier:SectionHeaderViewIdentifier];
if (sectionHeaderView == nil)
{
sectionHeaderView = [[AdminHeaderFooterView alloc] initWithReuseIdentifier:SectionHeaderViewIdentifier];
}
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(selectHeaderAction:)];
[sectionHeaderView addGestureRecognizer:tapGesture];
return sectionHeaderView;
}
-(void) selectHeaderAction :(UITapGestureRecognizer*) gestureRecognizer
{
AdminHeaderFooterView* cell = (AdminHeaderFooterView*)gestureRecognizer.view;
[self toggleSection:cell withSection: cell.section];
// how to pass clicked section to AYStepperView??
}
-(void)clicked :(NSUInteger) currentSection
{
NSLog(#"Stepper clicked %lu", currentSection);
}
StepperProgressTableViewCell.m
#implementation StepperProgressTableViewCell
#synthesize stepperView;
- (void)awakeFromNib {
[super awakeFromNib];
[self setUpViews];
}
- (void)setUpViews {
self.stepperView = [[AYStepperView alloc]initWithFrame:CGRectMake(0, 0 , [[UIScreen mainScreen] bounds].size.width, kFormStepperViewHeight) titles:#[#"Processing",#"Ready",#"Delivered", nil)]];
[self addSubview:self.stepperView];
}
AYStepperView.h
#property (nonatomic) NSUInteger currentSection;
AYStepperView.m
#protocol AYStepperViewDelegate <NSObject>
#required
- (void)clicked :(NSUInteger) currentSection;
#end
- (void)buttonPressed:(UIButton *)sender {
[stepperDelegate clicked : currentSection];
}
The cell should not need to know which row or section it is in; Your table view controller can find this easily, given a reference to the cell.
Your view controller should not set itself as the delegate of the stepper view. It should be a delegate of the cell. The cell should be the delegate of the stepper view. This is a bit more complicated but it maintains better separation of concerns and makes the whole thing cleaner.
AYStepperView.h
#protocol AYStepperViewDelegate <NSObject>
#required
- (void)clicked;
#end
AYStepperView.m
- (void)buttonPressed:(UIButton *)sender {
[stepperDelegate clicked];
}
StepperProgressTableViewCell.h
#protocol StepperProgressTableViewCellDelegate <NSObject>
#required
- stepperChanged: (StepperProgressTableViewCell) cell;
StepperProgressTableViewCell.m
-(void)awakeFromNib {
self.stepperView.delegate= self;
}
- (void)clicked {
[self.delegate stepperChanged: self];
}
AdminOrderViewController.m
#interface AdminOrderViewController : UIViewController <StepperProgressTableViewCellDelegate>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"StepperProgressCell";
StepperProgressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[StepperProgressTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.delegate= self;
return cell;
}
-(void)stepperChanged:(StepperProgressTableViewCell)cell {
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
// Now do something with indexPath.section
}
in cellForRowAtIndexPath set section to stepper view
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"StepperProgressCell";
StepperProgressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[StepperProgressTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.stepperView.delegate= self;
cell.stepperView.currentSection = indexPath.section;//set section value
return cell;
}
I'm using Objective-C. I want to push a view controller with a table view in it. When I use a normal table view cell, it works well. But when I use custom cells, It couldn't load and the simulator freeze.
Is there any problem can cause the simulator freeze when push a view controller? Some one can help me?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *simpleIdentifier = #"comment";
commentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentifier];
if (cell == nil){
cell = [[commentTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleIdentifier];
}
cell.username.text = [comments[indexPath.row] objectForKey:#"user"];
cell.textLabel.text = [comments[indexPath.row] objectForKey:#"content"];
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *customTableIdentifier=#"CustomCell";
CustomCell *cell=(CustomCell*)[tableView dequeueReusableCellWithIdentifier:customTableIdentifier];
if (cell==nil)
{
NSArray *nibs=[[NSBundle mainBundle]loadNibNamed:#"CustomCell" owner:self options:nil];
cell=[nibs objectAtIndex:0];
}
cell.yourLabelData.text = #"iOS";
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) //If you want to go view controller from particular index to other View Controller
{
//Here write the code for push the view
}
}
#pragma mark TableView delegete methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [Array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier=#"cellIdentifier";
SocialTableViewCell *cell=(SocialTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SocialTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
//write code for display data
return cell;
}
You need to register your custom cell in viewdidload
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[tableView registerNib:[UINib nibWithNibName:#"myCustomCell" bundle:nil] forCellWithReuseIdentifier:#"myCell"];
}
try these way..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *cellidentifier=[NSString stringWithFormat:#"%ld",(long)indexPath.row];
CustomCell *cell= (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellidentifier];
if (cell==nil)
{
cell=[[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
if (indexPath.row==0)
{
//Here change or edit specific custom cell if u want like i have UIImageview and i want to give it a specific image..
[cell.imgView1 setImage:imgsp1];
}
return cell;
}
i hope it helps..
in ViewController
.h
#property (strong, nonatomic) IBOutlet UITableView *tableViewData;
.m
#import "ViewController.h"
#import "CustomCell.h"
#interface ViewController ()
{
NSDictionary *dictComments;
NSArray *arrayUser;
NSArray *arrayContents;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
dictComments = [[NSDictionary alloc]initWithObjectsAndKeys:#"Jin",#"User",#"iOS Developer",#"Content", nil];
arrayUser = [[NSArray alloc]initWithObjects:[dictComments objectForKey:#"User"], nil];
arrayContents = [[NSArray alloc]initWithObjects:[dictComments objectForKey:#"Content"], nil];
}
//UITableViewDataSource Methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arrayUser.count;
//OR
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *customTableIdentifier=#"CustomCell";
CustomCell *cell=(CustomCell*)[tableView dequeueReusableCellWithIdentifier:customTableIdentifier];
if (cell==nil)
{
NSArray *nibs=[[NSBundle mainBundle]loadNibNamed:#"CustomCell" owner:self options:nil];
cell=[nibs objectAtIndex:0];
}
cell.labelUserName.text = [NSString stringWithFormat:#"%#",[arrayUser objectAtIndex:indexPath.row]];
cell.labelTextContent.text = [NSString stringWithFormat:#"%#",[arrayContents objectAtIndex:indexPath.row]];
//OR
cell.labelUserName.text = [NSString stringWithFormat:#"%#",[dictComments objectForKey:#"User"]];
cell.labelTextContent.text = [NSString stringWithFormat:#"%#",[dictComments objectForKey:#"Content"]];
return cell;
}
So i created a custom cell (using the .xib file) and linked it using a custom controller class and I also didn't forget to write in the cell identifier. I also gave the same cell identifier in my table view controller prototype cell. In the custom cell controller class I just have an outlet to a text label in the .h file. Below is the code for my table view controller. When I run the app, the custom cells are not displayed but there are cells there because i can click on them (but they are just white). What am I doing wrong, why aren't the custom cells displaying?
If I use the default cells (not custom), then everything works fine. So the problem is that I'm not using my custom cells correctly somewhere.
#import "ListmaniaTableViewController.h"
#import "ListmaniaTableViewCell.h"
#interface ListmaniaTableViewController ()
#property (strong, nonatomic) NSMutableArray *tasks; //of task object
#end
#implementation ListmaniaTableViewController
- (void)viewDidLoad{
[super viewDidLoad];
task *task1 = [[task alloc] init];
task1.taskDescription = #"TASK 1";
[self.tasks addObject:task1];
task *task2 = [[task alloc] init];
task2.taskDescription = #"TASK 2";
[self.tasks addObject:task2];
task *task3 = [[task alloc] init];
task3.taskDescription = #"TASK 3";
[self.tasks addObject:task3];
}
- (NSMutableArray *)tasks{
if(!_tasks){
_tasks = [[NSMutableArray alloc] init];
}
return _tasks;
}
/*- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
return self;
}*/
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"Add New Item"]) {
}
}
- (void)addNewTask:(task *)newTask{
[self.tasks addObject:newTask];
}
#pragma mark - Table view data source
- (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 [self.tasks count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
ListmaniaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ListmaniaCell" forIndexPath:indexPath];
if(!cell){
[tableView registerNib:[UINib nibWithNibName:#"ListmaniaTableViewCell" bundle:nil] forCellReuseIdentifier:#"ListmaniaCell"];
cell = [tableView dequeueReusableCellWithIdentifier:#"ListmaniaCell" forIndexPath:indexPath];
}
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(ListmaniaTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
task *task = [self.tasks objectAtIndex:indexPath.row];
NSString *taskLabel = task.taskDescription;
cell.taskLabel.text = taskLabel;
}
#end
But the
[self.tableView registerNib:[UINib nibWithNibName:#"ListmaniaTableViewCell" bundle:nil] forCellReuseIdentifier:#"ListmaniaCell"];
in viewDidLoad:.
The code you have in tableView:willDisplayCell:forRowAtIndexPath: method should be in tableView:cellForRowAtIndexPath: method. Like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
ListmaniaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ListmaniaCell" forIndexPath:indexPath];
if(!cell){
[tableView registerNib:[UINib nibWithNibName:#"ListmaniaTableViewCell" bundle:nil] forCellReuseIdentifier:#"ListmaniaCell"];
cell = [tableView dequeueReusableCellWithIdentifier:#"ListmaniaCell" forIndexPath:indexPath];
}
task *task = [self.tasks objectAtIndex:indexPath.row];
NSString *taskLabel = task.taskDescription;
cell.taskLabel.text = taskLabel;
return cell;
}
I figured out the problem. I had an outlet in the custom cell that was doubly linked. I also moved the line below into viewdidload as suggested by #dasdom
[self.tableView registerNib:[UINib nibWithNibName:#"ListmaniaTableViewCell" bundle:nil] forCellReuseIdentifier:#"ListmaniaCell"];
I am trying to create a UITableView application on Xcode 4.2. I just want each cells (ex. Cali) to push a new UIViewController when it's touched.
The issue I'm running into is whenever I press the cell it's not pushing the new view controller.
When I put a break point at the didSelectRowAtIndexPath method, I see 5 threads.
Here's my code:
#import <UIKit/UIKit.h>
#interface Adam : UITableViewController
{
NSMutableArray *states;
}
#end
#import "Adam.h"
#import "ViewController.h"
#implementation Adam
- (void)viewDidLoad
{
[super viewDidLoad];
states = [NSMutableArray arrayWithObjects:
#"cali",
#"ohio",
nil];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [states 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];
}
UILabel *cellLabel = (UILabel *)[cell viewWithTag:1];
[cellLabel setText:[states objectAtIndex:indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([[states objectAtIndex:indexPath.row] isEqual:#"cali"])
{
ViewController *cali = [[ViewController alloc] initWithNibName:#"cali" bundle:nil];
[self.navigationController pushViewController:cali animated:YES];
}
}
#end
First the TableViewCell has a textLabel property so you don't need to create your own label.
cell.textLabel.text= [states objectAtIndex:indexPath.row];
and is there a reason your deselecting the cell?Try deselecting it after you use the indexPath
And try:
if([states objectAtIndex:indexPath.row] isEqualToString:#"cali"]];){
}
i am trying to create a UITableView application on Xcode 4.2.
i just want each cells (ex. Cali) to push a new ViewController when its pushed
The issue I'm running into is whenever i press the cells its not pushing the new view controller
MY TableViewController.h
#import <UIKit/UIKit.h>
#interface Adam : UITableViewController
{
NSMutableArray *states;
}
#end
My Tableviewcontroller.m
#import "Adam.h"
#import "ViewController.h"
#implementation Adam
- (void)viewDidLoad
{
[super viewDidLoad];
states = [NSMutableArray arrayWithObjects:
#"cali",
#"ohio",
nil];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [states 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];
}
UILabel *cellLabel = (UILabel *)[cell viewWithTag:1];
[cellLabel setText:[states objectAtIndex:indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([[states objectAtIndex:indexPath.row] isEqual:#"cali"])
{
ViewController *cali = [[ViewController alloc] initWithNibName:#"cali" bundle:nil];
[self.navigationController pushViewController:cali animated:YES];
}
}
#end
cali.title = [states objectAtIndex: [indexPath row]];
[self.navigationController pushViewController:cali animated:YES];