Button click method is not working in collection view - objective-c

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]
}

Related

accesing contents of uitableview cell in another class

i have a class loginViewController which has table contents. i have loaded a custom cell to each row of the table. in method cellForRowAtIndexPath i have created a button for each row and asigned it a tag value. clicking on this button takes us to new view controller logininsidesViewController. Now in logininsidesViewController class i want to access the button tag i created earlier.what should i do?
loginViewController.m class
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
customcell *cell=(customcell*)[tableView dequeueReusableCellWithIdentifier:#"cellcstm"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(customActionPressed)
forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:#"add_black.png"] forState:UIControlStateNormal];
button.frame = CGRectMake(275 ,9, 25, 25);
[cell addSubview:button];
if (cell==nil)
{
//[tableView registerNib:[UINib nibWithNibName:#"customcell" bundle:nil] forCellReuseIdentifier:#"cellcstm"];
NSArray *arr12=[[NSBundle mainBundle]loadNibNamed:#"customcell" owner:self options:nil];
cell=[arr12 objectAtIndex:0];
}
for (int i=0; i<=indexPath.row; i++)
{
cell.selectionStyle=UITableViewCellEditingStyleNone;
}
if (indexPath.row==0)
{
cell.lbl.hidden=YES;
UILabel *l=[[UILabel alloc]init];
l.text=#"Login Templates";
l.frame=CGRectMake(100, 15, 180, 28);
[cell.contentView addSubview:l];
}
if (indexPath.row==1)
{
button.tag=1;
cell.img.image=[UIImage imageNamed:#"facebook.png"];
cell.lbl.text=#"Facebook";
[cell.contentView addSubview:button];
}
if (indexPath.row==2)
{
button.tag=2;
cell.img.image=[UIImage imageNamed:#"g+.png"];
cell.lbl.text=#"Google";
[cell.contentView addSubview:button];
}
if (indexPath.row==3)
{
button.tag=3;
cell.img.image=[UIImage imageNamed:#"youtube.png"];
cell.lbl.text=#"Youtube";
[cell.contentView addSubview:button];
}
if (indexPath.row==4)
{
button.tag=4;
cell.img.image=[UIImage imageNamed:#"twitter.png"];
cell.lbl.text=#"Twitter";
[cell.contentView addSubview:button];
}
if (indexPath.row==5)
{
button.tag=5;
cell.img.image=[UIImage imageNamed:#"flicker.png"];
cell.lbl.text=#"Flicker";
[cell.contentView addSubview:button];
}
else
{
cell .textLabel.text= [logarr objectAtIndex:indexPath.row];
}
return cell;
}
i want to access the button.tag in my logininsidesViewController class. plz help
In continuation with Viruss's answer you can create a property with integer type in logininsidesViewController class and pass that from button click.
-(IBAction) customActionPressed:(id)sender{
NSLog(#"button tag:%d",[sender tag]);
logininsidesViewController *objLoginInside = [[logininsidesViewController alloc] initWithNibName:#"logininsidesViewController" bundle:nil];
objLoginInside.buttonTag = sender.tag;
[self.navigationController pushViewController:objLoginInside animated:NO];
}
Add One target method to your buttons and access it with tags,
In cellForRow method Update to this.
[button addTarget:self action:#selector(customActionPressed:) forControlEvents:UIControlEventTouchUpInside];
You can access here with tag,
-(IBAction) customActionPressed:(id)sender{
NSLog(#"button tag:%d",[sender tag]);
}
You are adding your button on cell and again you're adding it on cell's contentView correct that is the problem.
If you want adding it in cell.contentView you must provide allocation in each if condition and then add it
Edit: Do smart coding
viewDidLoad()
[self.myTableView registerNib:[UINib nibWithNibName:#"CViewCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
arImage = #[#"facebook.png", #"g+.png", #"twitter.png", #"flicker.png" ];
arLables = #[#"Facebook", #"GooglePlus", #"Tweeter", #"Flicker"];
cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell * cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(customActionPressed) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:#"add_black.png"] forState:UIControlStateNormal];
button.frame = CGRectMake(275 ,9, 25, 25);
if (indexPath.row==0)
{
cell.lblText1.hidden=YES;
UILabel *l=[[UILabel alloc]init];
l.text=#"Login Templates";
l.frame=CGRectMake(100, 15, 180, 28);
[cell.contentView addSubview:l];
}
if ( indexPath.row > 0 && indexPath.row <= 5) {
button.tag = indexPath.row;
cell.imageView.image=[UIImage imageNamed:arImage[indexPath.row - 1]];
cell.textLabel.text=[arLables objectAtIndex:indexPath.row - 1];
[cell.contentView addSubview:button];
} else {
cell.textLabel.text= [logarr objectAtIndex:indexPath.row];
}
return cell;
}

How to change UIButton Background Image on UITableView didSelectRow without Reloading

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];
}

How to pass tableview cell as an argument and load the images only to the corresponding tableview cells she photo button is clicked in iOS

i ve got a custom tableview .i want to pass the tableview cell as a pramater to a function.I m using [cell.btnImages performSelector].while clicking the photos button it should load the images to the corresponding tableview cell .is it possible ?below is the code.when image button is clicked .i need to pass the cell as an argument...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"IpadTableViewCell";
IPadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"IpadTableViewCell" owner:nil options:nil];
for (id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[IPadTableViewCell class]])
{
cell = (IPadTableViewCell *)currentObject;
}
}
}
[cell.btnImages performSelector:#selector(imageButtonClicked:) onThread:nil withObject:cell waitUntilDone:nil];
return cell;
}
-(void)imageButtonClicked:(IPadTableViewCell *)tblCell
{
// opne image select dialog
CGFloat prevButtonXPosition = 17, pageButtonXPosition, pageButtonWidth = 120, pageButtonHeight = 130;
for (NSInteger i =1; i<= 9; i++) {
pageButtonXPosition = prevButtonXPosition ;
prevButtonXPosition = pageButtonXPosition + pageButtonWidth;
UIButton *pageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[pageButton addTarget:self action:#selector(onClickImage:) forControlEvents:UIControlEventTouchUpInside];
[pageButton setFrame:CGRectMake(pageButtonXPosition, 5, pageButtonWidth, pageButtonHeight)];
[pageButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"%i.png",i]] forState:UIControlStateNormal];
[pageButton setTag:i];
[cell.scrollView addSubview:pageButton];
}
UIButton *add=[UIButton buttonWithType:UIButtonTypeCustom];
[add addTarget:self action:#selector(onClickAdd:) forControlEvents:UIControlEventTouchUpInside];
[add setFrame:CGRectMake(prevButtonXPosition, 20, 80, 80)];
[add setBackgroundImage:[UIImage imageNamed:#"last.jpeg"] forState:UIControlStateNormal];
[add setTag:0];
[cell.scrollView addSubview:add];
[cell.scrollView setContentSize:CGSizeMake(prevButtonXPosition +80, 40)];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
You have defined the imageButtonClicked method in your tableview class but your calling for it in cell.btnImages. You should implement the imageButtonClicked method in the class of btnImages or call on it in self instead of cell.btnImages. If btnImages is of class UIButton you should instead do this:
[cell.btnImages addTarget:self action:#selector(imageButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
Or simply just implement the UITableViewDelegate method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

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 .

Cocoa-Touch – UITableView setEditing will not show specific cells

I've been struggling with this problem for a good 2 hours now and I can not get it to work.
I have a view controller with a table view on it with a list of items. When the user touches the EDIT button in the navigation bar the table view should enter "edit mode" and under the list of items one new cell should appear that should only be visible when it is in "edit mode".
So in my -tableView:cellForRowAtIndexPath: method I'm checking if (tableView.isEditing) to setup the cell. I've tried overriding -setEditing:animated: and doing [tableView reloadData] calls but I just can't get it to work.
This is what my -setEditing:animated: look like:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.groupDetailsTableView setEditing:editing animated:animated];
}
My -tableView:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
if (tableView.isEditing) {
// Other tableview code has been omitted
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
// Set the background view of the cell to be transparent
UIView *backView = [[UIView alloc] initWithFrame:CGRectZero];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[deleteButton setFrame:[cell.contentView frame]];
[deleteButton setFrame:CGRectMake(0, 0, cell.bounds.size.width-20, 44)];
[deleteButton setBackgroundImage:[UIImage imageNamed:#"redbutton.png"] forState:UIControlStateNormal];
[deleteButton setTitle:#"Delete Group" forState:UIControlStateNormal];
[deleteButton.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
[deleteButton.titleLabel setShadowColor:[UIColor lightGrayColor]];
[deleteButton.titleLabel setShadowOffset:CGSizeMake(0, -1)];
[deleteButton addTarget:self action:#selector(deleteGroup) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:deleteButton];
return cell;
}
}
And I'm calling [self setEditing:YES animated:YES]; from a method when the user touches the Edit button. I've also tried just calling [super setEditing:YES animated:YES]; and [self.groupDetailsTableView setEditing:YES animated:YES];
Found the answer myself at http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html#//apple_ref/doc/uid/TP40007451-CH10-SW19
Apparently -tableView:cellForRowAtIndexPath: doesn't get called automatically when calling -setEditing:animated:
So to have a new cell "appear" I set the alpha to 0.0 and then 1.0 after calling setEditing...