I am new to app development. In my app when I click on my table view cell, the cell is selected and didSelectRowAtIndexPath method is triggered irrespective of whether I clicked on image or cell. I want the image to be selected when I click on the image, not the cell. Thanks in advance.
Add UITapGestureRecognizer on your image view, and if you not want cell selection then off table view cell selection . on Tap function on image do what you want.
In your cellForRowAtIndexPath method, add a custom UIButton and assign the image as background image of the button. Then add target to the button and handle your code inside the button target method
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CellIdentifier"];
UIButton *imageButton = [[UIButton alloc] initWithFrame:<Give the frame you want>];
[imageButton setBackgroundImage:<your image> forState:UIControlStateNormal];
[imageButton addTarget:self action:#selector(imageButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:imageButton];
}
return cell;
}
Here the best method is to create a custom UITableViewCell which have imageButton as an instance variable instead of creating it inside cellForRowAtIndexPath method. Now in the imageButtonClicked method
-(void)imageButtonClicked:(UIButton *)sender {
UIImage *image = sender.currentBackgroundImage;
//Do the logic what you want here
}
Please give the frame of the button so that it is placed at the position you want.
I haven't tried out this code. If sender.currentBackgroundImage is not giving you the image, then create a custom button extending from UIButton. The custom button should have a UIImage variable as an instance variable. Assign the variable to the image you want when it is initialized(in cellForRowAtIndexPath method). And then you can access the image in the button target method
Related
I have created a custom UITableViewCell with a label on the far left (respects the parent margins) and it displays correctly. However, when I set an image using imageView.image (from the UITableViewCell), the label does not move to the right and so the label and the image are on top of each other. Any ideas how to make the label behave like the default label where it will move to the right to make way for the image?
If you create the cell from storyboard make sure you have set the UITableViewCell style to Custom.
Edit:
I saw your comment above.
If you don't wish to create a custom UITableViewCell class, you can still use default UITableViewCell with style set to Basic
So you can obtain its properties:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.text = #"Your text";
cell.imageView.image = [UIImage imageNamed:#"Your image name"];
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame: CGRectZero];
cell.accessoryView = mySwitch;
// Define the state of the switch because the cell will get recycled as you scroll.
BOOL isOn;
[mySwitch setOn:isOn];
return cell;
}
The other solution is mentioned as commented above: You have to subclass UITableViewCell, drag and drop 3 IBOutlets and reference them in .h file.
I used this code in cellForRowAtIndexPath method. When I click on button or scroll table this button is shown two times. Why is this button shown 2 times please help me?
UIButton *trashbtn=[[UIButton alloc]initWithFrame:CGRectMake(cell.frame.size.width-20, cell.frame.size.height-30, 20, 20)];
[trashbtn setImage:[UIImage imageNamed:#"editor_trash"] forState:UIControlStateNormal];
[trashbtn addTarget:self action:#selector(DeleteMyAssociate:) forControlEvents:UIControlEventTouchUpInside];
[trashbtn setTag:indexPath.row];
[cell addSubview:trashbtn];
You should firstly add a UITableViewCell within your table in IB. Then give an identifier e.g. "MyCellIdentifier" to that cell. Still on IB, add your outlets, to that cell e.g. the button, the textFields... You can initially set the button to be invisible. Then in the method cellForRowAtIndexPath, you do:
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyCellIdentifier"];
if(cell){
myButton.hidden = NO; //
myTextField.text = #"BlaBlaBla";
}
}
Hey I can give you a suggestion that use auto layout and storyboard to add button rather than adding it programmatically. That is more better and cleaner approach.
The problem you are facing is that cells are reused in tableviews, So the first time you create a button and add it to the cell, it appears once. But if it is dequeued, it already has the button, so when you add another one you end up with multiple buttons.
There are two ways to correct this.
Firstly, remove the button in the cell's prepareForReuse method, which is called just after the cell is reused.
Secondly, avoid creating custom views in the cellForTableView... method. Use a custom cell that already has the button. Now, you may ask, how do I hook up the action for the button if I do it this way? You can either provide a delegate method for your cell that calls back to your view controller, or you can pass your cell a block to perform when the button is clicked.
as far as i can get u already have a custom cell.. add a button to existing cell and create an IbOutlet for the same in customCell.h file
then in cellForRowAtIndexPath method access the button using its IBOutlet
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
customCell *cell =(customCell*) [tableView dequeueReusableCellWithIdentifier:#"customCell" forIndexPath:indexPath];
[cell.btn setTitle:#"blabla" forState:UIControlStateNormal];
[cell.btn setTag:indexPath.row];
[cell.btn addTarget:self action:#selector(method:) forControlEvents:UIControlEventTouchUpInside];
if(condition)
{
cell.btn.hidden=YES;
}
else
{
cell.btn.hidden = NO;
}
return cell;
}
Do not forget to register for the customCell class in your ViewController class
[tableName registerNib:[UINib nibWithNibName:#"customCell" bundle:nil] forCellReuseIdentifier:#"customCell"];
you can hide or unhide the button as per your requirements as well as add different actions to the button in each class.
Happy Coding..!!
Do vote it if my code was helpful for you.. ;)
I put a tableview in my storyboard, and filled the cell with a button with "0" as the tag. I populated the tableview using an array. Let's just say I have 20 elements in my array, that makes it 20 cells on the tableview. This is the code I'm using to give tags to the button on each cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == _tblCart) {
static NSString *cellIdentifier = #"CartCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
UIButton *btn = (UIButton *)[cell viewWithTag:0];
[btn setTag:indexPath.row];
[btn addTarget:self action:#selector(logTag:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
return nil;
}
I thought my code will work just fine, but actually there is something wrong. On cell 0-5, the button tag is correct. On the following cell the tag will reset again to 0. What am I doing wrong?
There is also another logic problem here, you are reusing table cells, some of which you've changed the tag of the button to something other than 0. So if you get a reused tablecell, there will come a time that the UIButton won't have a tag of zero and therefore you won't be able to change it correctly.
Don't set 0 as a tag identifier to views.
All UIViews have a tag by default 0.
So
[cell viewWithTag:0];
probably will return the contentView of the cell.
Both Flexicoder and l0gg3r are correct. Also, relying on button's tag as row identifier is a bit of a clunky workaround in my opinion. Consider this approach:
- (void)logTag:(UIButton *)sender {
NSIndexPath *indexPath = [_tblCart indexPathForRowAtPoint:[_tblCart convertPoint:sender.center fromView:sender]];
// do your stuff knowing which indexPath the button belongs to
}
I am building an app similar to iPhone photo app. I am able to show the images in a grid like view using PSUICollectionView. When I tap on a grid cell of collection view a checkbox image should appear. My problem is when I am using following code, I see that multiple random cells are being populated with check box images.
- (void)collectionView:(PSUICollectionView *)collectionView didSelectItemAtIndexPath: (NSIndexPath *)indexPath
{
NSLog(#"%# - %d", NSStringFromSelector(_cmd), indexPath.item);
ImageGridCell *cell = (ImageGridCell *)[collectionView cellForItemAtIndexPath:indexPath];
UIButton *chkboxBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[chkboxBtn setFrame:CGRectMake(60, 60, 30, 30)];
[chkboxBtn setImage:[UIImage imageNamed:#"Checkmark-iPhone.png"] forState:UIControlStateNormal];
[cell addSubview:chkboxBtn];
}
The problem is likely your custom cell has not implemented the prepareForReuse method. As a cell is reused, it may or may not have the check box, depending on if the check box was added on a previous use.
Several ways to address this. One simple way is to add a tag to the ckhboxBtn, and then remove the chkboxBton in the prepareForReuse method. E.g., when adding the check box, add the following:
[chkboxBtn setTag:100];
Then in your UICollectionViewCell class implementation, add/expand the prepareForReuse method:
-(void)prepareForReuse{
[[self viewWithTag:100] removeFromSuperview];
}
I'm currently working in a UITableView, each cell containing a button with a specific UIImage in background. I'd like to change this image when the button is clicked in a cell.
I have this code, working perfectly but when I click on a button, several buttons are modified - every 5 buttons, don't know why. Here's the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[myCell.favorite addTarget:self action:#selector(pressed_fav:) forControlEvents:UIControlEventTouchUpInside];
return myCell;
}
- (void)pressed_fav:(id)sender
{
UIButton *myButton = sender;
[myButton setBackgroundImage:[UIImage imageNamed:#"star.png"] forState:UIControlStateNormal];
}
myCell is an instance of a customized cell, containing an IBOutlet 'favorite' pointing to the UIButton.
Its because UITableView re-uses cell.
You must have some logic in cellForRowAtIndexPath method to identify the image of a button.
Thank you so much guy !
Actually, when I delete the line:
myCell = [tableView dequeueReusableCellWithIdentifier:#"cinemaCell"];
It does not display the cell (it's well allocated and initialised instead).