How to change UIButton Background Image on UITableView didSelectRow without Reloading - objective-c

I added a **UIButton** in UITableView Cell. And I wantto add a functionality on both clicks (UIButton and UITable - didSelectRow).
On Button Click I am able to change the button background image by Sender.
The Code is look like this:
-(void)tableButton_OnClick:(id)sender
{
UIButton *btn=(UIButton *)sender;
[btn setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
}
And the same functionality I want to add on UITable - didSelectRow without Reloading the table.

You have to make your own custom UITableViewCell with public #property UIButton. Then in didSelectRowAtIndexPath you may use something like this:
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
YourCell* yourCell = (YourCell*)[tableView cellForRowAtIndexPath:indexPath];
UIButton* button = yourCell.button;
[button setImage:[UIImage imageNamed:#"checked"] forState:UIControlStateNormal];
}

if ([[[myarry objectAtIndex:indexPath.row] objectForKey:#"key_name"] isEqualToString:#"1"])
{
[cell.button_my setBackgroundImage:[UIImage imageNamed:#"first.png"] forState:UIControlStateNormal];
}
els
{
[cell.button_my setBackgroundImage:[UIImage imageNamed:#"first_other.png"] forState:UIControlStateNormal];
}

STEPS - try this
while adding button on cell ,set the tag value to your button
eg :
btn.tag=indexpath.row;
cell.tag=indexpath.row;
On didSelectRowAtIndexPath, Create UIButton based on tag value
eg:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell *cell=(UITableViewCell *)[self.TableView viewWithTag:indexPath.row];
UIButton *btn=(UIButton *)[cell viewWithTag:indexPath.row];
[self tableButton_OnClick:btn];
}

Related

How can I show custom image in UITableView accessory view in Objective-C?

I am doing a project in Objective-C. My problem is now,
Suppose I have a UITableView I want when I select a row it can show image with current position of user and remaining rows no need to show the same image.
I tried with lot of examples but no use.
can anyone help me? please...
Upon row selection, you should grab a cell and create your custom accessory view with that cell:
UITableViewCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
BOOL bSelected = YES;
[self createAccessoryView:cell :bSelected];
Note however that bSelected bool above should ideally come from your model array, the array that holds your table view objects. Upon row selection, it will simply negate its value, such as:
bSelected = !bSelected;
Your createAccessoryView would look something like this:
- (void) createAccessoryView : (UITableViewCell *) cell :(BOOL)bSelected
{
UIImage * image = bSelected ? [UIImage imageNamed:#"check"] : [UIImage imageNamed:#"uncheck"];
cell.accessoryType = UITableViewCellAccessoryNone;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, 40, 40);
button.frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal];
//Uncomment below line if necessary
//[button addTarget:self action:#selector(accessoryButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
}
Off course, substitute your own image names above.
If you need more functionality in table view with some ready-made customisations (search, select, delete), take a look at this ViewController developed by me on github.
First of all place an image view in your cell, and put it as imgView.hidden = YES. When you select the cell, the image view of that cell to be unhide.
//Sample Code
IndexSelected = 99999 // In viewdidload
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
IndexSelected = indexpath.row;
[tbl reloaddata];
}
-(nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
if(IndexSelected == indexpath.row){
cell.imgView.hidden = NO;
}

Button click method is not working in collection view

I am creating a camera app. I am showing captured pictures in collection view. I placed a button to delete the particular picture. While running I am able to see the button to delete, but if I click the button it is not performing any action.
-(UICollectionViewCell *)collectionView:(UICollectionView *)
collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionViewCell *Cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
Cell.self.image_View.image=[self.imageArray objectAtIndex:indexPath.row];
UIButton *deleteButton = [[UIButton alloc]init];
deleteButton.frame = CGRectMake(80, 0, 20, 20);
//deleteButton.backgroundColor = [UIColor redColor];
[deleteButton setImage:[UIImage imageNamed:#"delete.png"] forState:UIControlStateNormal];
[deleteButton setTag:indexPath.row];
[deleteButton addTarget:self action:#selector(delete:) forControlEvents:UIControlEventTouchUpInside];
[Cell.self.image_View addSubview:deleteButton];
return Cell;
}
-(void) delete:(UIButton*)sender{
UIButton *btn = (UIButton *)sender;
[self.imageArray removeObjectAtIndex:btn.tag];
//reload your collectionview here
}
Can anyone help me?
i think you might be missing
[collectionView reloadData]
-(void) delete:(UIButton*)sender{
UIButton *btn = (UIButton *)sender;
[self.imageArray removeObjectAtIndex:btn.tag];
[collectionView reloadData]
}

UIButton below UITableView cannot be clicked

I have a Table View with APParallaxHeader. This creates a view behind the table, which acts as the header of it. My problem is, that I cannot click anything in this header. I've tried overriding -hitTest of the UITableView, but had no success.
Here is my view controller:
#import "TableViewController.h"
#import "UIScrollView+APParallaxHeader.h"
#implementation TableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
[button setTitle:#"Hello" forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor redColor]];
[button addTarget:self action:#selector(ping) forControlEvents:UIControlEventTouchUpInside];
[self.tableView addParallaxWithView:button andHeight:320];
}
- (void)ping
{
NSLog(#"%s", __PRETTY_FUNCTION__);
}
#end
EDIT:
My further investigation shows, that the problem can be reduced to a simple UIButton below a UITableView. How can you get the tap events with the button?

Button on UITableViewCell

i can create a button in tableview and i have a offers and in that offers i have products so i can generate all products as per offer in one table so i have a button to add cart when user add to cart it will show remove button its ok when i won't go to other view but when im came back it shows again add button so how can i get remove button after add and after came to this view please help me out thankyou
Translation
I want to create a button in a tableview. The button initial state should be 'Add to cart' and when the user clicks the button I want it to change to 'Remove'.
Translation
UIButton *btn = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
btn.frame = CGRectMake(230, 25, 73, 40) ;
btn.tag = indexPath.row+1;
btn.titleLabel.font = [UIFont boldSystemFontOfSize:15];
for (int i=0;i<[[[productsArray valueForKey:#"product"] valueForKey:#"productId"] count]; i++)
{
BOOL status=FALSE;
for (int j=0; j<[[appDelegate AddingCartArray] count]; j++)
{
NSString *pstr=[NSString stringWithFormat:#"%#",[[[productsArray valueForKey:#"product"] valueForKey:#"productId"] objectAtIndex:i]];
NSLog(#"pstr %#",pstr);
NSString *cstr=[NSString stringWithFormat:#"%#",[[[appDelegate AddingCartArray]valueForKey:#"productId"] objectAtIndex:j]];
NSLog(#"cstr %#",cstr);
if([[[productsArray valueForKey:#"product"] valueForKey:#"productId"] objectAtIndex:i] == [[[appDelegate AddingCartArray]valueForKey:#"productId"] objectAtIndex:j])
{
status =TRUE;
}
}
if(status)
{
[btn setBackgroundImage:[UIImage imageNamed:#"RemovetoCart.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(Remove:) forControlEvents:UIControlEventTouchUpInside];
}
else {
[btn setBackgroundImage:[UIImage imageNamed:#"adtoCart.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(addCartClicked:) forControlEvents:UIControlEventTouchUpInside];
}
}
[cell addSubview:btn];
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[btn release];
You should use method delegated from UITableView to add objects on UITableViewCell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// your button code generation here and assigning touch event to it
[cell.contentView addSubview:btn]
return cell;
}
after that you can create method which receives touchUpInside event and sets to sender (UIButton *) nesessary properties such as color or text.
btw: you shouldn't use absolute values in frame generating. Use cell.contentView.frame property for creating button frame CGRect .

how to change the button image inside the table cell while clicking the button in iphone

I have a tableview in my application. I created a button inside the table cell and assigned a background image to it. I wanted to change the background image when i click that button. I assigned an action for the button like this
-(void)goodBtnClicked:(id)sender {
[goodBtn setBackgroundImage:[UIImage imageNamed:#"camera.png"] forState:UIControlStateNormal];
}
But, the image is not getting replaced with the new image. Can any one help me with this?
You should make sure that your image is actually there.
-(void)goodBtnClicked:(id)sender {
UIImage *img = [UIImage imageNamed:#"camera.png"];
NSAssert(img, #"Image shouldn't be nil");
[goodBtn setBackgroundImage:img forState:UIControlStateNormal];
}
try with
-(void)goodBtnClicked:(id)sender {
[sender setBackgroundImage:[UIImage imageNamed:#"camera.png"] forState:UIControlStateNormal];
}
Go through all the subviews and locate your button then change background image
- (void)buttonPressed:(UIButton *)sender{
UITableViewCell *cell = (UITableViewCell *)sender.superview;
for (UIView *sub in cell.subviews) {
if([sub isMemberOfClass:[UIButton class]])
{
UIButton * button=[[UIButton alloc] init];
button=sub;
[button setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
}
}
}