I have two custom UItableViewCell with Same UIButton , How to determine from Which Cell UIButton is called ?
P.S. I Don’t want To use two different methods.
cellForRowAtIndexPath Method.
if (indexPath.row==1) {
static NSString *cellIdentifier = #“CustomeCell1“;
NewsFeedCell1 *cell = (NewsFeedCell1 *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.btnPrivacy.tag = indexPath.row;
[cell.btnPrivacy addTarget:self action:#selector(btnPrivacyClicked:) forControlEvents:UIControlEventTouchUpInside];
}
else
{
static NSString *cellIdentifier = #"CustomeCell2”;
NewsFeedCell2 *cell = (NewsFeedCell2 *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.btnPrivacy.tag = indexPath.row;
[cell.btnPrivacy addTarget:self action:#selector(btnPrivacyClicked:) forControlEvents:UIControlEventTouchUpInside];
}
Button click Methods.
-(IBAction)btnPrivacyClicked:(UIButton *)sender
{
NSLog(#"Privacy Clicked at : %ld",(long)sender.tag);
// Here Determine From Which Cell UiButton Is Clicked.
NewsFeedCell1 *cell = (NewsFeedCell1 *)[self.HomeTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:sender.tag inSection:0]];
// *** Use Following code to detect `Cell` ***
CGPoint buttonPosition = [button convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
[self.tableView cellForRowAtIndexPath:indexPath];
Set tag for cell.btnPrivacy.tag=indexPath.row;
and in button action you will get button btnPrivacy tag i.e sender.tag which is cell index.
Try with this.
Related
I want to pass NSIndexPath to #selector.
So I used following code, but there is no argument like withObject.
How can I do it?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
// User taps expanded row
if (selectedIndex == indexPath.row){
selectedIndex = -1;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
}
// User taps different row
if (selectedIndex != -1){
NSIndexPath *prevPath = [NSIndexPath indexPathForRow:selectedIndex inSection:1];
selectedIndex = (int)indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:prevPath, nil] withRowAnimation:UITableViewRowAnimationFade];
}
// User taps new row with unexpanded
selectedIndex = (int)indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
[favoriteButton addTarget:self action:#selector(favoritePressed:) withObject:indexPath forControlEvents:UIControlEventTouchDown];
[normalButton addTarget:self action:#selector(normalPressed) forControlEvents:UIControlEventTouchDown];
[halfButton addTarget:self action:#selector(halfPressed) forControlEvents:UIControlEventTouchDown];
}
-(void)favoritePressed:(NSIndexPath *)path{
}
There is a better way to do this. You can find the touch point on button click and convert that touch point in to your table frame, and through that point you can get indexpath.
-(void)favoritePressed:(UIButton *)sender{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableview];
NSIndexPath *indexPath = [self.tableview indexPathForRowAtPoint:buttonPosition];
}
Try like this:
-(void)favoritePressed:(UIButton *)sender{
//CustomCell : your custom UITableViewCell class name
CustomCell *cell = (CustomCell) [[sender superview] superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
}
NOTE:
If you set button in content view of CustomCell this will work for you otherwise you need to add other superview for getting the cell.
The code below may work for certain condition but your button may be a inside other view inside a cell. In that case you may not find the cell.
CustomCell *cell = (CustomCell) [[sender superview] superview];
It is better to use it like this.
- (void)btnFavoriteTapped:(UIButton *)sender {
id view = [sender superview];
while ([view isKindOfClass:[CustomCell class]] == NO) {
view = [view superview];
}
CustomCell *selectedCell = (CustomCell *)view;
NSIndexPath *indexPath = [self.tableView indexPathForCell:selectedCell];
}
I am using simple tableview and i have add button in every cell , The Problem is how to get button text from cell number second and any other cell number get button text i am using this code but its not working
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
UITableViewCell *cell = [tbl_view cellForRowAtIndexPath:indexPath];
for( UITableViewCell *getview in cell.subviews)
{
if([getview isKindOfClass:[UIButton class]])
{
NSLog(#"sdfsdfsd");
// UIButton *button = (UIButton *);
}
}
This code is set button in tableview cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
UILabel *fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 5, 250,20)];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
cell=[[UITableViewCell alloc] init];
}
cell.backgroundColor=[UIColor clearColor];
UIButton *button_chaeckbox = [UIButton buttonWithType:UIButtonTypeCustom];
button_chaeckbox.backgroundColor=[UIColor clearColor];
button_chaeckbox.frame = CGRectMake(10, 10, 15, 15);
if( [checkedArray containsObject:[NSString stringWithFormat:#"%ld",(long)indexPath.row]])
{
[button_chaeckbox setBackgroundImage:[UIImage imageNamed:#"checked_checkbox.png"] forState:UIControlStateNormal];
}else
{
[button_chaeckbox setBackgroundImage:[UIImage imageNamed:#"empty_box_b.png"] forState:UIControlStateNormal];
}
[button_chaeckbox setTitle:#"creaButtonname" forState:UIControlStateNormal];
button_chaeckbox.tag=indexPath.row;
[button_chaeckbox addTarget:self
action:#selector(checkboxAction:)
forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button_chaeckbox];
}
Please give me solution , i have try this code in ios 7 and ios 8 this is not working
Regards,
Nishant Chandwani
Do one thing give tag value for each button like
button_chaeckbox.tag=indexpath.row
in cellForRowAtIndexPath Method .
in checkboxAction do like this
NSString * titleText=sender.titleLabel.text
and you will check in button action method from which cell it is
like using conditions if(sender.tag == //your cell number second value//)
I think you want like this only .
Use this code for reuse cells in table view
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
cell=[[UITableViewCell alloc] init];
UILabel *fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 5, 250,20)];
cell.backgroundColor=[UIColor clearColor];
UIButton *button_chaeckbox = [UIButton buttonWithType:UIButtonTypeCustom];
button_chaeckbox.backgroundColor=[UIColor clearColor];
button_chaeckbox.frame = CGRectMake(10, 10, 15, 15);
[button_chaeckbox addTarget:self
action:#selector(checkboxAction:)
forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button_chaeckbox];
button_chaeckbox.tag=1001;
}
if( [checkedArray containsObject:[NSString stringWithFormat:#"%ld",(long)indexPath.row]])
{
[button_chaeckbox setBackgroundImage:[UIImage imageNamed:#"checked_checkbox.png"] forState:UIControlStateNormal];
}else
{
[button_chaeckbox setBackgroundImage:[UIImage imageNamed:#"empty_box_b.png"] forState:UIControlStateNormal];
}
}
Use this code for get button from cell
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
UITableViewCell *cell = [tbl_view cellForRowAtIndexPath:indexPath];
UIButton *button_chaeckbox=(UIButton*)[cell viewWithtag:1001]
You can try this
In cellForRowAtIndexPath
[cell.button setTitle:#"RAM" forState:UIControlStateNormal];
cell.button.tag=indexPath.row;
[cell.button addTarget:self action:#selector(cellBtn:) forControlEvents:UIControlEventTouchUpInside];
Selector method
-(void)cellBtn :(UIButton *)sender
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
AlertCustomCell *cell = [tableview cellForRowAtIndexPath:indexPath];
NSString *user = cell.button.titleLabel.text;
NSLog(#"Text of button is :%#",user);
}
I hope it will work
let me point out one thing. if you want to traverse through all the subviews of the table cell you have to make for( UITableViewCell *getview in cell.subviews) to for( UIView *getview in cell.subviews)
The code would look like this
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
UITableViewCell *cell = [tbl_view cellForRowAtIndexPath:indexPath];
for( UIView *getview in cell.subviews)
{
if([getview isKindOfClass:[UIButton class]])
{
UIButton *button = (UIButton *)getView;
NSLog #("%#",button.titleLabel.text)
}
}
I would recommend making a UITableViewCell subclass and making the button a a property on that subclass. Then in your code would look something like this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
UILabel *fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 5, 250,20)];
if (cell == nil) {
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.backgroundColor=[UIColor clearColor];
UIButton *button_chaeckbox = [UIButton buttonWithType:UIButtonTypeCustom];
button_chaeckbox.backgroundColor=[UIColor clearColor];
button_chaeckbox.frame = CGRectMake(10, 10, 15, 15);
if( [checkedArray containsObject:[NSString stringWithFormat:#"%ld",(long)indexPath.row]])
{
[button_chaeckbox setBackgroundImage:[UIImage imageNamed:#"checked_checkbox.png"] forState:UIControlStateNormal];
} else {
[button_chaeckbox setBackgroundImage:[UIImage imageNamed:#"empty_box_b.png"] forState:UIControlStateNormal];
}
[button_chaeckbox addTarget:self
action:#selector(checkboxAction:)
forControlEvents:UIControlEventTouchUpInside];
[cell setCellButton:button_chaeckbox];
return cell;
}
And then to get the button:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
CustomTableCell *cell = (CustomTableCell *)[tbl_view cellForRowAtIndexPath:indexPath];
UIButton * yourButton = cell.CellButton
I created custom UITableView with a button. User open homepage when click the button.
Homepage Address of the buttons are Json parsing. In other words, the homepage address is different for each button.
I don't know how can I setting a different address for each button.
This is my Source Code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"customCell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}
[cell.title setText:[[list objectAtIndex:indexPath.row] objectForKey:#"title"]];
[cell.date setText:[[list objectAtIndex:indexPath.row] objectForKey:#"date"]];
NSString *listSite = [[list objectAtIndex:indexPath.row] objectForKey:#"site"];
UIButton *cellButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cellButton addTarget:self action:#selector(selectSite:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
and this is my SourceCode in CustomTableCell
(IBAction) selectSite:(id)sender {}
Assign your indexpath to UIButton's tag like below
cellButton.tag = indexpath.row
and use this tag in your UIButton's Action method
(IBAction) selectSite:(id)sender {
int value = [sender tag];
}
and set it back to cellForRowAtIndexpath method
I created one prototype cell. Cell has one label and one button. I have given tag's for both.
Now i want to detect which button is clicked from 10 cells.
Previously we were differentiating that based on tag. But how to do this with prototype cell.
My code for cell creation is as follows:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cellIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cellIdentifier"];
}
UIButton *stopStartButton = (UIButton *)[cell viewWithTag:103];
UILabel *chargingLabel = (UILabel *)[cell viewWithTag:102];
}
-(IBAction)stopStartButtonClicked:(id)sender
{
NSLog(#"Button clicked");
}
You can use button.titleLabel.tag for differentiate your button and at action time you can compare with same tag.
second option is with your button action. you can append event so that is also provide you all information regarding your button.
For example you just set
stopStartButton.titleLabel.tag=1;
-(IBAction)stopStartButtonClicked:(id)sender
{
NSLog(#"Button clicked %d",sender.titleLabel.tag);
}
I think the best way is to find what cell does actually has button that was tapped. You can find it by calculating x origin point of button.
- (IBAction)stopStartButtonClicked:(id)sender
{
CGPoint pointInTable = [button convertPoint:button.bounds.origin toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:pointInTable];
}
If you have indexPath, you can get cell:
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
I want to add some buttons to custom cell, when I click the button and other buttons also change. How can I click a button without changing the other buttons?
enter code here
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"cell";
SViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[SViewTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier];
}
cell.cellButton.tag = indexPath.row;
[cell.cellButton addTarget:self action:#selector(clickMe:)
forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)clickMe:(UIButton *)sender
{
SViewTableViewCell *cell = (SViewTableViewCell *)[[sender superview]superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
if (sender.tag == 0) {
if ([sender.titleLabel.text isEqualToString:openStr]) {
[sender setTitle:closeStr forState:UIControlStateNormal];
}
else{
[sender setTitle:openStr forState:UIControlStateNormal];
}
}
}
I think it's a issue about tableView cell reuse again, you should set the button title in cellForRow to keep reuse cell have a correct title, you should try code like this:
openDict is a NSMutableArray you should define, and this just a example.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"cell";
SViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[SViewTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier];
[cell.cellButton addTarget:self action:#selector(clickMe:)
forControlEvents:UIControlEventTouchUpInside];
}
NSString *key = [NSString stringWithFormat:#"%d-%d", indexPath.row, indexPath.section];
if (openDict[key] && [openDict[key] boolValue]) {
[sender setTitle:openStr forState:UIControlStateNormal];
}
else{
[sender setTitle:closeStr forState:UIControlStateNormal];
}
cell.cellButton.tag = indexPath.row;
return cell;
}
- (void)clickMe:(UIButton *)sender
{
SViewTableViewCell *cell = (SViewTableViewCell *)[[sender superview]superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
if (sender.tag == 0) {
NSString *key = [NSString stringWithFormat:#"%d-%d", indexPath.row, indexPath.section];
if (openDict[key] && [openDict[key] boolValue]) {
openDict[key] = #(0);
[sender setTitle:closeStr forState:UIControlStateNormal];
}
else{
openDict[key] = #(1);
[sender setTitle:openStr forState:UIControlStateNormal];
}
}
}
Please set tag for each button in custom tableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"cell";
SViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[SViewTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier];
}
UIButton *button1 = (UIButton *)[cell viewWithTag:1];
UIButton *button2 = (UIButton *)[cell viewWithTag:2];
[button1 addTarget:self action:#selector(clickMe:)
forControlEvents:UIControlEventTouchUpInside];
[button2 addTarget:self action:#selector(clickMe:)
forControlEvents:UIControlEventTouchUpInside];
return cell;
}
To add a button to a UITableViewCell you should do it at the time you configure the cell in cellForRowAtIndexPath - this is after you get the cell to reuse. If you want to add a button to a small number of cells you should cause that cell to be reloaded using UITableView's – reloadRowsAtIndexPaths:withRowAnimation: method or reloadData to give you the opportunity to reload all cells.
Any subview you add to a cell should be added to the cell's contentView as a subview. You should remove all such views in prepareForReuse or you could find cell reuse makes extra buttons appear in cells you don't want to have buttons. Because of the need to prepare cells for reuse it is probably best to subclass UITableViewCell and to provide methods on the subclass to add buttons.
Another way is to register multiple kinds of cells for reuse with the table view, choosing what kind of cell to retrieve and use based on the data backing your table view. This is as simple as using registerClass:forCellReuseIdentifier: once for each kind of cell, then applying right reuse identifier in dequeueReusableCellWithIdentifier:forIndexPath: - you'll still be subclassing the cell objects but you configure them further in the class rather than at the time they are dequeued.
Use the code below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = #"cell";
SViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[SViewTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.cellButton.tag = indexPath.row;
[cell.cellButton addTarget:self action:#selector(clickMe:)
forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)clickMe:(UIButton *)sender {
SViewTableViewCell *cell = (SViewTableViewCell *)[[[sender superview]superview] superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
[self keepSelectionRemainsSameForIndexPath:indexPath andSender:sender];
}
- (void)keepSelectionRemainsSameForIndexPath:(NSIndexPath *)indexPath andSender:(UIButton *)sender {
if (sender.tag == indexPath.row) {
if ([sender.titleLabel.text isEqualToString: openStr]) {
[sender setTitle:closeStr forState:UIControlStateNormal];
} else {
[sender setTitle: openStr forState:UIControlStateNormal];
}
}
}