How to update custom image if table cell is selected outside controller - iphone

I need to update image in Firstviewcontroller if cell is selected in tableviewcontroller.
My application Navigation base . Firstviewcontroller contains button event. tap on button i am firing tableview. In tableview i am selecting cell and updating checkmark in left side , It is working fine . My question is when cell is selected same time i need to update different custom image in viewcontroller for indicating cell is selected. With ref. i got image cell selecting in inside of tableview.
here it is Appdelegate
#interface FertilityAppAppDelegate : NSObject <UIApplicationDelegate>
{
Fertility *updateImage;
}
#property (nonatomic, retain) Fertility *updateImage;
#implementation
#synthesize updateImage;
#interface Fertility : UIViewController
{
UIImageView *imgView;
FertilityAppAppDelegate *Appdelegate;
}
#property(nonatomic,retain) FertilityAppAppDelegate *Appdelegate;
#property (nonatomic, retain) UIImageView *imgView;
Viewcontroller for image update
#implementation Fertility
#synthesize imgView,Appdelegate;
- (void)viewDidLoad
{
[super viewDidLoad];
Appdelegate = (FertilityAppAppDelegate*)[[UIApplication sharedApplication] delegate];
}
- (void)changeImg:(UIImage*)image
{
imgView.image = image;
}
- (void)assignObjToTableViewClass
{
Appdelegate.updateImage = self;
}
Tableview controller
#interface Menstruation : UITableViewController
{
FertilityAppAppDelegate *Appdelegate;
}
#property (nonatomic, retain) FertilityAppAppDelegate *Appdelegate;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIImage *image = [UIImage imageNamed:#"bar.png"];
[Appdelegate.updateImage changeImg:image];
selectedRow = indexPath.row;
[self.tableView reloadData];
}
- (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];
[[cell imageView] setImage:[UIImage imageNamed:#"Tick.png"]];
}
[[cell imageView] setHidden:YES];
if (indexPath.row == selectedRow)
{
[[cell imageView] setHidden:NO];
}
NSString *cellValue = [MenstruationArray objectAtIndex:indexPath.row];
cell.text = cellValue;
return cell;
}
How to display in Firstviewcontroller. the above image is Button with red color bar in bottom of button.
When i click this button in Firstviewcontroller firing for Menstruation table view, same time i need to update image bar to Firstviewcontroller.

if you want to change the image in the another view controller then you need to assign the image in the didSelectRowAtIndexPath method.
For that you need to access the specific view controller object(that is already using not the new one).
There are no tutorials, for these type of things.
I will send some code, please follow like that.
First of all you have to declare ImgViewController class object in the AppDelegateClass. Then only you can use.
Then use the following code. I have written this code for understanding.
#interface ImgViewController : UIViewController{
UIImageView *imgView;
AppDelegateClass *delegate;
}
#implementation ImgViewController
- (void)viewDidLoad{
delegate = (AppDelegateClass*)[[UIApplication sharedApplication]delegate];
delegate.imageViewControllerObj = self; // Or you can call the method
//[self assignObjToTableViewClass];
}
- (void)changeImg:(UIImage*)image{
imgView.image = image;
}
- (void)assignObjToTableViewClass{
delegate.imageViewControllerObj = self;
}
#end
#interface TableViewControllerClass : UIViewController{
AppDelegateClass *delegate;
}
#implementation TableViewControllerClass
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UIImage *image = //Give what ever image you want to change
[delegate.imageViewControllerObj changeImg:image];
}
#end
Regards,
Satya

Related

Custom Cell can't display in Table view in ios 6

#import "MasterViewController.h"
#import "DetailViewController.h"
#interface MasterViewController ()
#end
#implementation MasterViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Master", #"Master");
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
}
}
return self;
}
- (void)dealloc
{
[_detailViewController release];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100.0;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil)
{
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.cellDate.text=#"date";
cell.cellDescription.text =#"Description";
cell.cellImageview.image = [UIImage imageNamed:#"facebook.png"];
cell.cellTitle.text = #"Title";
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
if (!self.detailViewController) {
self.detailViewController = [[[DetailViewController alloc] initWithNibName:#"DetailViewController_iPhone" bundle:nil] autorelease];
}
[self.navigationController pushViewController:self.detailViewController animated:YES];
}
else
{
}
}
#end
.h file
#import <UIKit/UIKit.h>
#import "CustomCell.h"
#import "XMLStringFile.h"
#class DetailViewController;
#interface MasterViewController : UITableViewController{
}
#property (strong, nonatomic) DetailViewController *detailViewController;
#property(strong,nonatomic)CustomCell *customCell;
#end
CustomCell.h
#import <UIKit/UIKit.h>
#interface CustomCell : UITableViewCell{
IBOutlet UIImageView *cellImageview;
IBOutlet UILabel *cellTitle;
IBOutlet UILabel *cellDescription;
IBOutlet UILabel *cellDate;
}
#property (retain, nonatomic) IBOutlet UIImageView *cellImageview;
#property (retain, nonatomic) IBOutlet UILabel *cellTitle;
#property (retain, nonatomic) IBOutlet UILabel *cellDescription;
#property (retain, nonatomic) IBOutlet UILabel *cellDate;
#end
CustomCell.m
#import "CustomCell.h"
#implementation CustomCell
#synthesize cellDate;
#synthesize cellDescription;
#synthesize cellImageview;
#synthesize cellTitle;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
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
This is my both class
here Problem is in tableview data of the customcell cant Display onlt white screen.
Here i work in ios with masterdetails view template..
here i have also added m file in compile source
Customcell.xib size is 300X100
Here my output
My xib file as below
please help me to solve problem
if(cell == nil)
{
NSArray *outlets = [NSArray arrayWithArray:[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil]];
for(id obj in outlets)
{
if([obj isKindOfClass:[UITableViewCell class]])
{
cell = (CustomCell *)obj;
}
}
}
If your CustomCell is made via XIB then Write this Code where your cell is nil.
EDIT:-
This may be the cause - I think you haven't changed your CustomCell's class name. Follow these steps -
Take a XIB with name CustomCell.xib then delete its view.
Take a UITableViewCell and set its height and structure according to you.
Select File's Owner and change its class name to CustomCell, Same thing do with UITableViewCell... select it and change its class name to CustomCell.
Now connect all subView's IBOutLets.
Note:- Select IBOutLets by right clicking on UITableViewCell not from File's Owner.
Do the following in viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
UINib *nib = [UINib nibWithNibName:#"Customcell" bundle:nil];
[[self tableView] registerNib:nib forCellReuseIdentifier:#"CustomCell"];
}
If the cell is designed in a nib then you need to load the cell from the nib.
This actually has support in UIKit since iOS 5
What you need to do is register your nib with the UITableView.
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:#"Customcell" bundle:nil]
forCellReuseIdentifier:#"CustomCell"];
//...
}
Now your tableView:cellForRowAtIndexPath: just needs to look like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = (id)[tableView dequeueReusableCellWithIdentifier: CellIdentifier];
cell.cellDate.text = #"date";
cell.cellDescription.text = #"Description";
cell.cellImageview.image = [UIImage imageNamed:#"facebook.png"];
cell.cellTitle.text = #"Title";
return cell;
}
NB
Make sure to remember to set CustomCell as the reuseIdentifier in the xib
Some other observations
Really you should be using ARC for a new project.
#synthesize is implicit so not required in most cases
It's probably a bad idea to #import "CustomCell.h" in MasterViewController.h.
You should move this to the .m and use a forward declaration in the .h instead - #class CustomCell;
You also do not need to declare the backing ivars for properties as these will also be generated by the compiler e.g. you don't need to declare the following
#interface CustomCell : UITableViewCell {
IBOutlet UIImageView *cellImageview;
IBOutlet UILabel *cellTitle;
IBOutlet UILabel *cellDescription;
IBOutlet UILabel *cellDate;
}

transform tableview

I want to images display for cell. but this code display, only white tableview.Why? Please tell me. (I use not storyboard.)
TableCell.h
#import <UIKit/UIKit.h>
#interface TableCell : UITableViewCell <UITableViewDelegate,UITableViewDataSource>
#property (nonatomic,retain) UITableView *tableCellView;
#property (nonatomic,retain) NSArray *cellArray;
-(NSString *)reuseIdentifier;
#end
TableCell.m
#import "TableCell.h"
#implementation TableCell
#synthesize tableCellView;
#synthesize cellArray;
-(NSString *)reuseIdentifier
{
return #"cell";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [cellArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableCellView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
for (UIImageView *view in cell.subviews)
{
[view removeFromSuperview];
}
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
imageView.image = [UIImage imageNamed:[cellArray objectAtIndex:indexPath.row]];
imageView.contentMode = UIViewContentModeCenter; // 画像サイズを合わせて貼る
CGAffineTransform rotateImage = CGAffineTransformMakeRotation(M_PI_2);
imageView.transform = rotateImage;
[cell addSubview:imageView];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 220;
}
#end
TableViewController.h
#import <UIKit/UIKit.h>
#class TableCell;
#interface TableViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
#property (nonatomic, retain) UITableView *tableView;
#property (nonatomic, retain) TableCell *tableViewCell;
#property (nonatomic, retain) NSArray *titlesArray;
#property (nonatomic, retain) NSArray *peopleArray;
#property (nonatomic, retain) NSArray *thingsArray;
#property (nonatomic, retain) NSArray *fruitsArray;
#property (nonatomic, retain) NSArray *arrays;
#end
TableViewController.m
#import "TableViewController.h"
#import "TableCell.h"
#interface TableViewController ()
#end
#implementation TableViewController
#synthesize tableView;
#synthesize tableViewCell;
#synthesize titlesArray;
#synthesize peopleArray;
#synthesize thingsArray;
#synthesize fruitsArray;
#synthesize arrays;
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
[self.view addSubview:tableView];
titlesArray = [[NSArray alloc] initWithObjects:#"People", #"Things", #"Fruits", nil];
peopleArray = [[NSArray alloc] initWithObjects:#"Gardener.png", #"Plumber.png", #"BusinessWoman.png", #"BusinessMan.png", #"Chef.png", #"Doctor.png", nil];
thingsArray = [[NSArray alloc] initWithObjects:#"StopWatch.png", #"TrashCan.png", #"Key.png", #"Telephone.png", #"ChalkBoard.png", #"Bucket.png", nil];
fruitsArray = [[NSArray alloc] initWithObjects:#"Pineapple.png", #"Orange.png", #"Apple.png", nil];
arrays = [[NSArray alloc] initWithObjects:peopleArray, thingsArray, fruitsArray, nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [arrays count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [titlesArray objectAtIndex:section];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 220;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
TableCell *cell = (TableCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"TableViewCell" owner:self options:nil];
CGAffineTransform rotateTable = CGAffineTransformMakeRotation(-M_PI_2);
tableViewCell.tableCellView.transform = rotateTable;
tableViewCell.tableCellView.frame = CGRectMake(0, 0, tableViewCell.tableCellView.frame.size.width, tableViewCell.tableCellView.frame.size.height);
tableViewCell.cellArray = [arrays objectAtIndex:indexPath.section];
tableViewCell.tableCellView.allowsSelection = YES;
cell = tableViewCell;
}
return cell;
}
#end
AppDelegate.m
TableViewController *tvc = [[TableViewController alloc] init];
self.window.rootViewController = tvc;
UITableViewCell by default already has support for images. You do not need to to add a new UIImageView as a subview ...
So instead of this:
for (UIImageView *view in cell.subviews)
{
[view removeFromSuperview];
}
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
imageView.image = [UIImage imageNamed:[cellArray objectAtIndex:indexPath.row]];
imageView.contentMode = UIViewContentModeCenter; // 画像サイズを合わせて貼る
CGAffineTransform rotateImage = CGAffineTransformMakeRotation(M_PI_2);
imageView.transform = rotateImage;
[cell addSubview:imageView];
Write this:
cell.imageView.image = [UIImage imageNamed:[cellArray objectAtIndex:indexPath.row]];
Oh, I now notice that LOTS OF OTHER STUFF is wrong with your code! Sorry to say that. Other stuff you need to change:
Copy this method from your TableCell to your TableViewController: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath and remove the existing one
DELETE the TableCell class
I see you're also doing rotation... but leave that out for now. First try to make it work and THEN worry about rotation of the image.
Can I maybe advise you to read a book on iOS development? It seems as if you don't know what you're doing at all :(
But I do like to help you out. Can you maybe put all your code on http://www.github.com and invite me as a collaborator? My username is tomvanzummeren. I can make this app work for you.
your numberOfSectionsInTableView method might return 0. check the value of [arrays count]. And one more thing is you are not created UITableViewCell in proper way. That might be the problem too. Check this link for creating Custom UITableViewCell
Each UITableViewCell has an image support if it's style is default.
cell.imageView.image = yourImageView.image;
You don't need to do anything else.
You can change Image settings with changing yourImageView settings if you want.

Delegation in iOS

I have 2 ViewControllers, in 1st - TableView and in 2nd - button with label on it. When I click on the button in 2nd ViewController I need to go back on TableView and set in
cell.detailTextLabel.text
text from label on the button.
Here is my code, but it does not work:
ViewController.h:
#import <UIKit/UIKit.h>
#import "TestControl.h"
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, myTestProtocol>
{
TestControl *myProtocol;
}
#property (strong, nonatomic) IBOutlet UITableView * tableTest;
#end
ViewController.m:
#import "ViewController.h"
#import "TestControl.h"
#implementation ViewController
#synthesize tableTest;
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBarHidden = YES;
myProtocol = [[TestControl alloc]init];
myProtocol.delegate = self;
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TestControl * test = [[TestControl alloc] initWithNibName:#"TestControl" bundle:nil];
[self.navigationController pushViewController:test animated:YES];
}
- (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];
}
switch (indexPath.row) {
case 0:
cell.textLabel.text = #"Firs Cell";
cell.detailTextLabel.text = myProtocol.myLabel.text;
break;
case 1:
cell.textLabel.text = #"Second Cell";
break;
case 2:
cell.textLabel.text = #"Third Cell";
break;
default:
break;
}
return cell;
}
#end
TestControl.h:
#import <UIKit/UIKit.h>
#protocol myTestProtocol <NSObject>
#end
#interface TestControl : UIViewController
{
UILabel *myLabel;
}
#property (nonatomic, assign) id <myTestProtocol> delegate;
#property (strong, nonatomic) IBOutlet UILabel *myLabel;
- (IBAction)testButton:(id)sender;
#end
TestControl.m:
#implementation TestControl
#synthesize myLabel;
#synthesize delegate;
- (IBAction)testButton:(id)sender
{
myLabel.text = #"TEXT LABEL";
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
}
What is the problem in this???
A couple of things....
You are creating two different TestControl objects, setting the delegate for one of them and pushing the other one, so the one handling the button tap has no delegate.
The delegate logic would work better the other way around. That is, TestControl should have the code that communicates with its delegate rather than the delegate "pulling" from TestControl.

Multple buttons in CustomCell. Which button in which row is clicked?

im working on a student project where I need to know which button in which row is clicked.
Each row has different labels, one like button and one dislike button.
I do know how to use NSIndexPath.row to detect which row is pressed, but In this case I need to know both which row and which buttton in that row is pressed.
I have searched all over SO for a good solution to detecting which row a button is clicked when having multiple buttons per row.
From what I understand reading the other posts, using button.tag can be very unpredictable so I rather use a different method if possible.
I have seen many apps that have this implemented, so there has to be an optimal way to do this. Does anyone have any good suggestions?
Code:
SearchCell.h
#interface SearchCell : UITableViewCell {
IBOutlet UIButton *likebutton2;
IBOutlet UIButton *dislikebutton2;
}
#property (nonatomic,retain) IBOutlet UILabel *track_label;
#property (nonatomic,retain) IBOutlet UILabel *artist_label;
#property (nonatomic,retain) IBOutlet UILabel *album_label;
#property (nonatomic,retain) IBOutlet UIButton *likebutton2;
#property (nonatomic,retain) IBOutlet UIButton *dislikebutton2;
#property (nonatomic, copy) void (^onButton)(UIButton *button);
- (void)buttonAction:(UIButton *)sender;
#end
SearchCell.m
#import "SearchCell.h"
#import "RootViewController.h"
#implementation SearchCell
#synthesize likebutton2, dislikebutton2, track_label, artist_label, album_label;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code.
}
return self;
}
- (void)buttonAction:(UIButton *)sender
{
self.onButton(sender);
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state.
}
- (void)dealloc {
[super dealloc];
}
#end
cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *uniqueIdentifier = #"searchCell";
SearchCell *cell = nil;
Search *currentSearch = nil;
cell = (SearchCell *) [self.tableView dequeueReusableCellWithIdentifier:uniqueIdentifier];
if (tableView == [[self searchDisplayController] searchResultsTableView]) //Just from some previous debugging
{
currentSearch = [[searchxmlParser searchhits] objectAtIndex:indexPath.row];
}
if(!cell)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"SearchCell" owner:nil options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[SearchCell class]]) {
cell = (SearchCell *)currentObject;
[cell.likebutton2 addTarget:cell action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.dislikebutton2 addTarget:cell action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
cell.likebutton2.tag = 1;
cell.dislikebutton2.tag = 2;
break;
}
}
}
cell.onButton = ^(UIButton *theButton) {
[self handleButton:theButton indexPath:indexPath];
}
cell.track_label.text = [currentSearch track];
cell.artist_label.text = [currentSearch artist];
return cell;
}
Thank you for helping :)!
Why not do something like the following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *reuseIdentifier = #"MyCell";
MyCustomCell *cell = (id)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil) {
cell = [[[MyCustomCell alloc] init] autorelease];
[cell.firstButton addTarget:self action:#selector(firstButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell.secondButton addTarget:self action:#selector(secondButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
}
[cell.firstButton setTag:indexPath.row];
[cell.secondButton setTag:indexPath.row];
// other cell setup...
return cell;
}
- (void)firstButtonPressed:(id)sender {
NSInteger cellRow = [sender tag];
// do things...
}
- (void)secondButtonPressed:(id)sender {
NSInteger cellRow = [sender tag];
// do things...
}
This assumes that you only have one section in your UITableView, it's a bit more complicated to do this with multiple sections.
I think using blocks would be a good way to go, still using tags as in #EllNeal's solution:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *reuseIdentifier = #"MyCell";
MyCustomCell *cell = (id)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil) {
cell = [[[MyCustomCell alloc] init] autorelease];
[cell.button1 addTarget:cell action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.button2 addTarget:cell action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
cell.button1.tag = 1;
cell.button2.tag = 2;
}
cell.onButton = ^(UIButton *theButton) {
[self handleButton:theButton indexPath:indexPath];
}
return cell;
Then your handler where you tell which button was pressed would be like this:
- (void)handleButton:(UIButton *)button indexPath:(NSIndexPath *)indexPath
{
//Use tag of button to identify which button was tapped, as well as the indexPath
}
Your cell code would look a bit like this:
#interface MyCustomCell
...
- (void)buttonAction:(UIButton *)sender
#property (nonatomic, copy) void (^onButton)(UIButton *button);
...
#end
#implementation
...
- (void)buttonAction:(UIButton *)sender
{
self.onButton(sender);
}
...
#end
Hope this helps!
Use the .tag property, it's very reliable and predictable.
If you think otherwise, show some code.

Problem with secondary delegate

I have two UITableView, each one has a different delegate in the same UIViewController.
I'm trying to call control in another delegate, but it failed.
Brief:
mainViewController contains UITableView (with UITableViewDelegate and UITableViewDataSource) + UIImageView.
secondaryTable contains UITableView (with UITableViewDelegate and UITableViewDataSource).
I want to call UIImageView from secondaryTable.
In .h
#interface secondaryTable : UIViewController <UITableViewDataSource,UITableViewDelegate>
#end
#interface mainViewController : UIViewController <UITableViewDataSource,UITableViewDelegate> {
#property (nonatomic,retain) IBOutlet UIImageView *navbar;
#property (nonatomic,retain) IBOutlet UITableView *Table1;
#property (nonatomic,retain) IBOutlet UITableView *Table2;
#end
In .m
#implementation secondaryTable
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier2";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
[cell.textLabel setFont:[UIFont boldSystemFontOfSize:12.0]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.textAlignment = UITextAlignmentCenter;
}
if (indexPath.row == 0) cell.textLabel.text =#"A";
if (indexPath.row == 1) cell.textLabel.text =#"B";
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Selected");
mainViewController *myController = [[mainViewController alloc] init];
[myController.navbar setHidden:NO];
}
#end
#implementation mainViewController
- (void)viewDidLoad {
Table1.delegate = self;
Table1.dataSource = self;
secondaryTable *myDelegate = [secondaryTable alloc];
Table2.delegate = myDelegate;
Table2.dataSource = myDelegate;
#end
}
It types "Selected" in console log, but it won't show control.
Why can't it call navbar control in mainViewController?
I would suggest doing this by having both tables point to the same delegate but, within your delegate methods, select behaviour based on which table it is. Example:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == firstTable)
{
// Do appropriate things here
}
else if (tableView == secondTable)
{
// do things for table #2 here
}
else
{
assert(no); // error checking
}
}