These are the files I am working on:
ChecklistViewController.h
ChecklistViewController.m
ChecklistTableViewCell.h
ChecklistTableViewCell.m
ChecklistTableViewCell.xib
DetailViewController.h
DetailViewController.m
DetailViewController.xib
I have a ChecklistViewController which displays a UITableView. Because I needed a different layout for the UITableViewCell, I have subclass UITableViewCell. I have created a nib file ChecklistTableViewCell.xib which has a UITableViewCell that contains a label, button and switch view. i have linked the UITableViewCell to a custom class ChecklistTableViewCell.
I have linked up the necessary outlets from the nib file to ChecklistTableViewCell class, and in ChecklistViewController cellForRowAtIndexPath I'm able to display label text.
In the UITableViewCell class file ChecklistTableViewCell, I have also linked and implemented a IBAction method that will be called when the button is clicked. When this method is called, how do I open DetailViewController as a popup?
Here is the snippet of my IBAction method:
-(IBAction)showPopup:(id)sender
{
NSLog(#"popup");
DetailViewController *vp = [[DetailViewController alloc] init];
vp.modalPresentationStyle = UIModalPresentationFormSheet;
vp.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
vp.view.superview.frame = CGRectMake(0, 0, 540, 620);
}
rdelmar is spot on with his comment. Because presentViewController:animated:completion is a method in the view controller, your ChecklistTableViewCell needs to make the view controller aware of the button click.
You have 2 choices:
Assuming your data source is your view controller, in your view controller:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ChecklistTableViewCell *cell = ...;
cell.button.tag = indexPath.row; // or whatever appropriate
[cell.button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)buttonTapped:(UIButton *)button
{
DetailViewController *vc = ...; // get your "popup" accordingly using button.tag
[self presentViewController:vc animated:YES completion:nil];
}
Or you can declare a protocol that your view controller adheres to:
// ChecklistTableViewCell.m
#protocol ChecklistTableViewCellDelegate
- (void)buttonTapped:(ChecklistTableViewCell *)sender;
#end
#implementation ChecklistTableViewCell : UITableViewCell
{
...
- (IBAction)showPopup:(id)sender // maybe change the name to something more appropriate
{
if ([self.delegate respondsToSelector:#selector(buttonTapped:)])
{
[self.delegate buttonTapped:self];
}
}
...
}
// ChecklistViewController.m
#implementation ChecklistViewController : ChecklistTableViewCellDelegate
{
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ChecklistTableViewCell *cell = ...;
cell.tag = indexPath.row; // or whatever appropriate
cell.delegate = self;
...
return cell;
}
- (void)buttonTapped:(ChecklistTableViewCell *)sender
{
DetailViewController *vc = ...; // get your "popup" accordingly using sender.tag
[self presentViewController:vc animated:YES completion:nil];
}
...
}
If you want to show a detail view controller as pop up in iPad there is a control called UIPopoverController.
How to use UIPopoverController?
In .h file
UIPopoverController *popoverController;
In .m file
DetailviewController * objViewController = [[DetailviewController alloc]initWithNibName:#"DetailviewController" bundle:nil];
obj.delegate = (id )self; // if DetailViecontroller has any delegate.
popoverController = [[UIPopoverController alloc] initWithContentViewController:objViewController];
popoverController.popoverContentSize = CGSizeMake(320.0, 400.0);
UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:theRow inSection:1]];
CGRect rect=CGRectMake(cell.bounds.origin.x+600, cell.bounds.origin.y+10, 50, 30);
[popoverController presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
Related
I am using storyboard. I have a table view with 1 Prototype cell, and have four rows in table view. Each cell has a label,image view and a button. Now I want to show different data on next View Controller say(DetailsVc), each time when button is pressed.
Row1- Button click will open DetailsVC with some data.
Row2- Button click will open DetailsVC with different data.
and same process for remaining rows.
Any suggestions.
#implementation DetailsVC
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
EmpEngmntTableViewCell *emp;
if((emp.viewGalleryBtn.tag = 1))
{
NSLog(#"tag num for 1st row is %ld",(long)emp.viewGalleryBtn.tag);
self.title = #"Team Building Session";
}
}
The best is to use a delegate pattern. I added an answer here, you can use it for example.
Here is an example enclosed:
Custom Cell .h Class:
#protocol DetailVCProtocol <NSObject>
-(void)loadDetailScreenWithIndex:(NSInteger)rowIndex;
#end
#property (nonatomic, retain) id<DetailVCProtocol> delegate;
#property (nonatomic, retain) IBOutlet UIButton *btn;
Then Synthesize it in .m file
Add this in m file:
-(IBAction)loadDetails:(id)sender
{
// ..... blah blah
[self.delegate loadDetailScreenWithIndex:btn.tag];
}
The viewcontroller that loads this cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// create cell here
cell.btn.tag = indexPath.row;
cell.delegate = self;
}
-(void)loadDetailScreenWithIndex:(NSInteger)rowIndex
{
// Cretae DetailVC with parameter `rowIndex` to populate data
[self presentViewController:detailVC animated:YES completion:nil];
}
In function - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
use this code
cell.btn.tag = indexPath.row;
[cell.btn addTarget:self action:#selector(btnTpd:) forControlEvents:UIControlEventTouchUpInside];
- (IBAction)btnTpd:(id)sender
{
UIButton *btn = (UIButton *)sender;
\\You can get which button is pressed by btn.tag
\\Here call to next ViewController and pass data
}
In cellForRowAtIndexPath
cell.optionsBtn.tag=indexPath.row;
[cell.optionsBtn addTarget:self action:#selector(showMyOptionBtn:) forControlEvents:UIControlEventTouchUpInside];
-(void)showMyOptionBtn:(UIButton *)sender
{
EventDetailViewController *EventDetail=[[EventDetailViewController alloc]init];
EventDetail.buttonTag=sender.tag;
[self.navigationController pushViewController:EventDetail animated:NO];
}
In EventDetailViewController class
if (self.buttonTag==1) {
// your condation
}
else
{
//your condation
}
I'm trying to animate a few items in a custom UITableViewCell when a user presses a button in the cell. I have set addTarget: and added a method to animate the items in the cell. I've set a tag for the button so I can get the index. In the method, I call cellForRowAtIndexPath: to get the cell the button was called on. I'm casting the object returned by cellForRowAtIndexPath: to my custom cell. After I have the custom cell, I perform the animations on the objects. The problem is the animations aren't happening. When I try setting a property on one of the items in the cell, it doesn't work either. The custom cell is not returning nil so I'm not sure of the issue. What am I doing wrong?
Here is the code I'm using to call the animations
-(void)favButtonPressed:(id)sender{
FavoritesCell *favCell = (FavoritesCell *)[self tableView:self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:[sender tag] inSection:0]];
[UIView animateWithDuration:0.2 animations:^{
favCell.picButton.alpha = 0.5;
} completion:nil];
}
One way u can do it by using custom delegate like below, hope this helps u .
i took an example of your case go through this hope this helps u
in custom cell calss define delegate protocol like below
in FavoritesCell.h
#import <UIKit/UIKit.h>
#class FavoritesCell;
#protocol CellActionDelegate <NSObject>
- (void)doAnimationForCell:(FavoritesCell *)cell forButton:(UIButton *)picButton; //in this u can pass the cell itself
#end
#interface FavoritesCell : UITableViewCell
#property (nonatomic,retain)UIButton *picButton; //i am doing a simple test, say this is your pic button
#property (nonatomic,assign) id<CellActionDelegate>cellDelegate;
#end
and in FavoritesCell.m file
#import "FavoritesCell.h"
#implementation FavoritesCell
#synthesize cellDelegate;
#synthesize picButton = _picButton;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
_picButton = [[UIButton alloc]initWithFrame:CGRectMake(20, 3, 100, 100)];
//set the property // for test
[_picButton addTarget:self action:#selector(favButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[_picButton.layer setCornerRadius:50];
[_picButton.layer setMasksToBounds:YES];
[_picButton.layer setBorderColor:[[UIColor blackColor]CGColor]];
[_picButton.layer setBorderWidth:5];
_picButton.backgroundColor = [UIColor greenColor];
[self.contentView addSubview:_picButton];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)favButtonPressed:(UIButton *)sender
{
//or you can do the animation hear only no need to delegate to controller form cell
if([self.cellDelegate respondsToSelector:#selector(doAnimationForCell:forButton:)])
{
[self.cellDelegate doAnimationForCell:self forButton:sender]; //call this method
}
}
#end
in ViewController.h file
#import <UIKit/UIKit.h>
#import "FavoritesCell.h" //import it
#interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,CellActionDelegate> //confirms to delegate
//.... other stuffs
in ViewController.m file
//...other stuffs
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FavoritesCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CELL"];
if(cell == nil)
{
cell = [[FavoritesCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CELL"];
}
cell.cellDelegate = self; //set the deleagte to this class
[cell.picButton setImage:[UIImage imageNamed:#"Two-Red-Flower-.jpg"] forState:UIControlStateNormal]; //set it hear or in custom cell itself
//...other logics
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 130.0f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
//define the delegate method
- (void)doAnimationForCell:(FavoritesCell *)cell forButton:(UIButton *)picButton //this the picButton
{
//you can get index path of cell
//you can get the tapped button of the cell
//then do your animation
[UIView animateWithDuration:0.2 animations:^{
picButton.alpha = 0.5;
} completion:nil];
}
I think you want something like this:
-(void)favButtonPressed:(id)sender
{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.myTableView];
// or try this one alternatively if the above line give wrong indexPath
//CGPoint buttonPosition = [sender convertPoint:((UIButton *)sender).center toView:self.myTableView];
NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:buttonPosition];
// I assume "self" here is your view controller
FavoritesCell *favCell = (FavoritesCell *)[self.myTableView cellForRowAtIndexPath:indexPath];
[UIView animateWithDuration:0.2 animations:^{
favCell.picButton.alpha = 0.5;
} completion:nil];
}
Right now I have a kind of pop-up selector so that the user can pick an option on my screen. Please see this sample project to see what I mean.
I'm using the same method as this sample, except that I have three different buttons where the user will select. My problem is that I am using this code:
- (void)itemSelectedatRow:(NSInteger)row
{
NSLog(#"row %lu selected", (unsigned long)row);
[self.selectSeverity setTitle:[self.severityArray objectAtIndex:row] forState:UIControlStateNormal];
[self.selectStatus setTitle:[self.statusArray objectAtIndex:row] forState:UIControlStateNormal];
[self.selectGender setTitle:[self.genderArray objectAtIndex:row] forState:UIControlStateNormal];
}
...and it is changing the name of the button for all three every time the user selects one. So for example, if the user taps the "selectSeverity" button, and chooses the item on row three, the name for the selectStatus and selectGender button will also change to the item on row three on its own corresponding array.
What I need to do is somehow separate this method so the button's title changes only when a row has been selected in its own array: how can I do this?
More information:
I have these tableViews embedded in a Navigation Controller:
statusViewController.m/.h
genderViewController.m/.h
pickerViewController.m/.h (this corresponds to the Severity button)
Each have a delegate with a separate ID, but the same content:
#protocol correspondingViewControllerDelegateHere <NSObject>
#required
- (void)itemSelectedatRow:(NSInteger)row;
#end
#interface correspondingViewControllerHere : UITableViewController
#property (strong, nonatomic) NSArray *correspondingArrayHere;
#property (assign, nonatomic) id<statusViewControllerDelegate> delegateIdHere;
#end
Each have the same content in the .m file as well, but correspond to their own delegates, arrays, etc.
#implementation statusViewController
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.statusData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"CellIdentifierHere";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [self.statusData objectAtIndex:indexPath.row];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([self.delegateIdHere respondsToSelector:#selector(itemSelectedatRow:)]) {
[self.delegateIdHere itemSelectedatRow:indexPath.row];
[self dismissViewControllerAnimated:YES completion:nil];
}
}
#end
In manualViewController.h (this is the view where they are implemented) I have declared the delegates.
Lastly, in the file manualViewController.m, I have the following code to implement them:
- (IBAction)showinfo:(id)sender
{
UINavigationController *navigationController = (UINavigationController *)[self.storyboard instantiateViewControllerWithIdentifier:#"pickerVC"];
pickerViewController *tableViewController = (pickerViewController *)[[navigationController viewControllers] objectAtIndex:0];
tableViewController.severityData = self.severityArray;
tableViewController.navigationItem.title = #"Triage Severity Levels";
tableViewController.delegate1 = self;
[self presentViewController:navigationController animated:YES completion:nil];
}
- (IBAction)showStatus:(id)sender {
UINavigationController *navigationController = (UINavigationController *)[self.storyboard instantiateViewControllerWithIdentifier:#"statusVC"];
statusViewController *tableViewController = (statusViewController *)[[navigationController viewControllers] objectAtIndex:0];
tableViewController.statusData = self.statusArray;
tableViewController.navigationItem.title = #"Triage Severity Levels";
tableViewController.delegate3 = self;
[self presentViewController:navigationController animated:YES completion:nil];
}
- (IBAction)showGender:(id)sender {
UINavigationController *navigationController = (UINavigationController *)[self.storyboard instantiateViewControllerWithIdentifier:#"genderVC"];
genderViewController *tableViewController = (genderViewController * [[navigationController viewControllers] objectAtIndex:0];
tableViewController.genderData = self.genderArray;
tableViewController.navigationItem.title = #"Triage Severity Levels";
tableViewController.delegate2 = self;
[self presentViewController:navigationController animated:YES completion:nil];
}
SO as I said before, I think the code lies in the itemSelectedAtRow. Can someone help me separate this method so that the choice of one item does not affect the choice of another item?
Thanks
One solution is to change the protocol to require three different methods
#protocol correspondingViewControllerDelegateHere <NSObject>
#required
- (void)itemSelectedSeverityAtRow:(NSInteger)row;
- (void)itemSelectedStatusAtRow:(NSInteger)row;
- (void)itemSelectedGenderAtRow:(NSInteger)row;
#end
Then implement the three methods in the manualViewController and call the appropriate method from each of the other view controllers.
I have a UITableView which has different types of files and folders in it, Right i have set a method that passes to another view controller once clicked on a row. What i need is that once a row is clicked on it checks what kind of a file is in the row and then connects to different uiviewcontrollers on the basis of that.
My UiTableView has two items in each cell
A Cell Label & Cell Detail Text Label
The DetailTextLabel holds the Subject type
i.e. Folder (For Folders) & File (For Files like jpeg. , png., etc.)
I want to use the if condition in the didselectrowatindexpath to distinguish between file and folder
You can do that by checking value of cell.detailTextLabel.text like following:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *str = cell.detailTextLabel.text;
if ([str isEqualToString:#"Folder"])
{
// Open Folder Detail View. For Example:
FolderViewController* objVC = [[FolderViewController alloc] initWithNibName:#"FolderViewController" bundle:nil];
[self.navigationController pushViewController:objVC animated:YES];
}
else if ([str isEqualToString:#"File"])
{
// Open File Detail View. For Example:
FileViewController* objVC = [[FileViewController alloc] initWithNibName:#"FileViewController" bundle:nil];
[self.navigationController pushViewController:objVC animated:YES];
}
}
in .h file
#import "FolderViewController.h"
#import "FileViewController.h"
#interface mainViewController : UIViewController {
FolderViewController *folderViewObj;
FileViewController *fileViewObj;
}
in .m file
- (void)viewDidLoad {
[super viewDidLoad];
folderViewObj = [[FolderViewController alloc] initWithNibName:#"FolderViewController" bundle:nil];
fileViewObj = [[FileViewController alloc] initWithNibName:#"FileViewController" bundle:nil];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell = [tblObj cellForRowAtIndexPath:indexPath];
NSString *lblText = cell.DetailTextLabel.text;
if ([lblText isEqualToString:#"Folder"]) {
[self.navigationController pushViewController:folderViewObj animated:YES];
}
else if ([lblText isEqualToString:#"File"])
{
[self.navigationController pushViewController:fileViewObj animated:YES];
}
}
-(void) dealloc {
[folderViewObj release];
[fileViewObj release];
[super dealloc];
}
using this way, object of FolderViewController and FileViewController is created only once not all time when user can select the row of uitableview.
I followed the SimpleDrillDown app example in the docs for a workout app that shows workout names in the first UITableView and exercises in the second UITableView.
My app is in Dropbox here: http://db.tt/V0EhVcAG
I used storyboards, have prototype cells, but when I load in simulator the first UITableView won't let me click and go to the detail UITableView. The disclosure indicator chevrons don't load. The app builds successfully and there are no formal errors.
My tableviews are in a navigation controller, my segue and prototype cells are all named accordingly in storyboard.
SpitfireViewController.m
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dataController countOfList];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"WorkoutCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Workout *workoutAtIndex = [dataController objectInListAtIndex:indexPath.row];
cell.textLabel.text = workoutAtIndex.title;
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showExercises"]) {
NSIndexPath *selectedRowIndex = [self.tableView indexPathForSelectedRow];
DetailViewController *detailViewController = [segue destinationViewController];
detailViewController.workout = [dataController objectInListAtIndex:selectedRowIndex.row];
}
}
DetailViewController.m
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [workout.exercises count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ExerciseCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [workout.exercises objectAtIndex:indexPath.row];
return cell;
}
AppDelegate.h
#import <UIKit/UIKit.h>
#class DataController;
#class SpitfireViewController;
#interface SpitfireAppDelegate : UIResponder <UIApplicationDelegate>
{
UIWindow *window;
SpitfireViewController *spitfireViewController;
DataController *dataController;
}
#property (strong, nonatomic) UIWindow *window;
#end
AppDelegate.m
#import "SpitfireAppDelegate.h"
#import "SpitfireViewController.h"
#import "DataController.h"
#implementation SpitfireAppDelegate
#synthesize window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
spitfireViewController = [[SpitfireViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:spitfireViewController];
DataController *controller = [[DataController alloc] init];
spitfireViewController.dataController = controller;
[window addSubview:[navController view]];
[self.window makeKeyAndVisible];
return YES;
}
#end
If you have tableview cell segue selection issue.
You need to check first that your tableview allows selection
You do not need to implement UITableViewDelegate and UITableViewDataSource when using "Static cells"
You need to add a segue for the accessory action + a segue for the selection.
When using Storyboards, you'll typically set the storyboard file name in the Project settings under the info tab. Once you've selected your storyboard there, you can basically delete everything except the return YES part of the application:didFinishLaunchingWithOptions: method. It's all taken care of for you in the storyboard.
EDIT:
Here's where you set the storyboard:
Also make sure that your view controller is set as the initial view controller:
I think this is your problem:
spitfireViewController = [[SpitfireViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:spitfireViewController];
DataController *controller = [[DataController alloc] init];
spitfireViewController.dataController = controller;
The spitfireViewController you create here is not the one in your storyboard, it's a new one. You should delete all this code, as you already have a spitfireViewController embedded in a navigation controller that you made in the storyboard. You should set the data controller for spitfireViewController in it's viewDidLoad method:
DataController *controller = [[DataController alloc] init];
self.dataController = controller;
Where is your "didSelectRowAtIndexPath" method?
I would recommend putting that in and then triggering [self performSegueWithIdentifier:] to trigger the segue using the data in the table cell as the sender.