I add button in UITableView cell. My code is
UIButton *btnView = [[UIButton buttonWithType:UIButtonTypeContactAdd] retain];
btnView.frame = CGRectMake(280.0, 8.0, 25.0, 25.0);
//[btnView setImage:[UIImage imageNamed:#"invite.png"] forState:UIControlStateNormal];
[btnView addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btnView];
[btnView release];
My button action is
- (void)action:(id)sender
{
}
I want to know, how to get current table row index when click. I want to delete table row when click on button.
Edit::
I found a way for delete with animation
[listOfItems removeObjectAtIndex:indexPath.row];
[self.tableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
I believe one approach is to store the row number in the button tag
btnView.tag = [indexPath row]
in the action
- (void)action:(id)sender
{
UIButton * btn = (UIButton)sender;
int row = btn.tag;
}
I usually use this method:
- (IBAction)buttonAction:(id)sender {
UIButton *button = sender;
UIView *contentView = [button superview];
UITableViewCell *cell = (UITableViewCell *)[contentView superview];
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
//Do something
}
Depending on the layout of your UITableViewCell it is possible that you have to traverse deeper into your TableViewCell Views.
http://nshipster.com/associated-objects/ Use an associated object of a NSNumber surrounding the row or the index path itself as the associated object
//when you create the button
objc_setAssociatedObject(button, associationKeyForButton, [NSNumber numberWithInteger:indexPath.row], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
//in the touch up inside handler to get the row back.
NSNumber* rowNumber = objc_getAssociatedObject(sender, associationKeyForButton);
Related
I have a table view and in each row there is a add button on click the new row adding below i want that onclick the row has one text field in it and a user can enter a texh on it can you help me how to add a text field in the adding row.
here is my code
-(void)buttonclicked:(UIButton *)sender
{
UIButton *btn = (UIButton *)[self.view viewWithTag:sender.tag];
NSLog(#"clickedCell=%i", btn.tag);
[[NSUserDefaults standardUserDefaults]setValue:[NSString stringWithFormat:#"%i", btn.tag] forKey:#"btntag"];
[self.choices insertObject:#"newrow" atIndex:btn.tag+1];
[self.tableView reloadData];
}
and the table view method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
button.tag = indexPath.row;
button.frame = CGRectMake(280.0, 10, 25, 30.0); // x,y,width,height
[button addTarget:self action:#selector(buttonclicked:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:cell action:nil];
longPress.delegate = self;
[cell addGestureRecognizer:longPress];
int count = 0;
if(self.editing && indexPath.row != 0)
count = 1;
NSLog([NSString stringWithFormat:#"%i,%i",indexPath.row,(indexPath.row-count)]);
// Set up the cell...
if(indexPath.row == ([_choices count]) && self.editing)
{
cell.textLabel.text = #"ADD";
return cell;
}
NSString *choice = [self.choices objectAtIndex:indexPath.row];
cell.textLabel.text = choice;
return cell;
}
try like this may be it helps to you,
1.take one global variable type integer for example position.
2.when you click on particular button -(void)buttonclicked:(UIButton *)sender method will call in this method you will assign the btn.tag value to the position .
3.after that you are reloading the table view in the tableview cellForRowAtIndexPath method take one condition like
if(position==indexpath.row)
{
//add your textfield here
}.
hi i am implementing a table in which i am getting request i have list of students to which i want to add as my room mate i have two button accept and reject the problem is here when i click on accept button i want that row will be deleted from the array and also i want to shift the deleted row value into the table of nextview controller in case of accept request . and in case of reject button cliked only want to delete the row not to move the value of cell into next view controller i am implementing this project by using storyboard can anybody have idea about that plase share me this is the code for cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"DefaultCell"];
if(cell == nil) {
cell =[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"DefaultCell"];
}
acceptreq = [UIButton buttonWithType:UIButtonTypeRoundedRect];
acceptreq.frame =CGRectMake(170, 10, 60, 30);
rejectreq = [UIButton buttonWithType:UIButtonTypeRoundedRect];
rejectreq.frame =CGRectMake(240, 10, 60, 30);
[acceptreq setTitle:#"Accept" forState:UIControlStateNormal];
[rejectreq setTitle:#"Reject" forState:UIControlStateNormal];
[acceptreq addTarget:self action:#selector(Acceptrequest:) forControlEvents:UIControlEventTouchUpInside];
[rejectreq addTarget:self action:#selector(Rejectrequest:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:acceptreq];
[cell addSubview:rejectreq];
[acceptreq setTag:indexPath.row];
cell.textLabel.text = [myarray objectAtIndex:indexPath.row];
return cell;
}
In short you can delete selected Row with clicking UIbutton like bellow:-
-(IBAction)Acceptrequest:(id)sender
{
UIButton *btn =(UIButton*)sender;
[myarray removeObjectsAtIndexes:btn.tag];
[tableView deleteRowsAtIndexPaths:myarray withRowAnimation:UITableViewRowAnimationAutomatic];
[tableVIew reloadData];
}
You need to implement this method:
[tableView deleteRowsAtIndexPaths:arrayOfIndexPathsToDelete withRowAnimation:UITableViewRowAnimationAutomatic];
I add UIButton in the cell Dynamically
and change UIButton image on click of UIBarButtonItem
how i can change?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"Display"] ;
[cell.textLabel setText:[NSString stringWithFormat:#"%#" ,[arrItemList objectAtIndex:indexPath.row]]];
UIButton *btnCheck = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnCheck.frame = CGRectMake(6.0f, 2.0f, 32.0f, 25.0f);
[btnCheck setTitle:#"" forState:UIControlStateNormal];
[btnCheck addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btnCheck];
return cell;
}
//Here IBAction for Bar Button Item
//when ever user click on BarButton then set the image of all btnCheck
-(IBAction)bbtnCheck:(id)sender
{
}
Assuming that your question is how to change the image of a button w/in every cell of your table when a bar button is clicked, I suggest the following:
Within tableView:cellForRowAtIndexPath, add:
btnCheck.tag = MY_BUTTON_TAG_NUMBER;
Then, when the bar button item is clicked, loop through the visible cells and use the button's tag to identify it and update its image.
-(IBAction)bbtnCheck:(id)sender {
for (UITableViewCell *cell in self.tableView.visibleCells) {
UIButton *button = [cell viewWithTag:MY_BUTTON_TAG_NUMBER];
[button setImage:[UIImage imageNamed:SOME_NEW_IMAGE] forState:UIControlStateNormal];
}
}
In that bbtnCheck you have to get the instance of that tableView (i.e. particular cell)..Then after you can get buttons added on that particular cell..
Please Check this....
-(IBAction)bbtnCheck:(id)sender
{
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:YourRow inSection:YourSection]; // you have to set Row and Section according to your requirement
UITableViewCell *cellNew = [self.tblComments cellForRowAtIndexPath:indexpath];
for (UIButton *btn in [cellNew.contentView subviews])
{
if ([btn isKindOfClass:[UIButton class]])
{
[btn setImage:[UIImage imageNamed:#"yourImage.png"] forState:UIControlStateNormal];
}
}
}
i am creating button dynamically. and creation action methods by
[newButton addTarget:self action:#selector(goToNew:)forControlEvents:UIControlEventTouchUpInside];
i want to send argument (indexpath.row) from tableview , but not want to use tag.. because i need that tag will remain same for all the buttons , how can i pass argument in button action ?
actually i am adding button in each tableview cell , and i want action for all those buttons , but if i use tag = indexpath.row and and use it with action, it works but problem of overlaying button happen.hence i want tag would be constant.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UIButton *btn;
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addTarget:self action:#selector(goToNew:) forControlEvents:UIControlEventTouchUpInside];
btn.tag = 55;
[cell.contentView addSubview:btn];
}
else {
btn = (id)[cell.contentView viewWithTag:55];
}
return cell;
}
- (void) goToNew:(id)sender
{
UIButton *b = (UIButton *)sender;
UITableViewCell cell = (UITableViewCell)[b superview];
int row = [msgTableView indexPathForCell:cell].row;
(#"row is :::%d",row);
}
By doing following code, you will get the indexPath.row and no need to set the button tag.
- (IBAction)goToNew:(id)sender
{
UIButton *btn = (UIButton*) sender;
UITableViewCell *cell = (UITableViewCell*) [btn superview];
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
int row = indexPath.row;
}
You can pass another argument as set title of button using different forstate.Here you can pass indexpath as title of button.like
btn.tag=10;
[btn setTitle:[NSString stringWithFormat:#"%d",indexpath.row] forState:UIControlStateDisabled];
[btn addTarget:self action:#selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
and you will received argument as
-(void)btnClick:(id)sender
{
UIButton* b = (UIButton*) sender;
NSLog(#"tag=%d",b.tag);
//Below line will got indexpath in string formate..
NSLog(#"title =%#",[b titleForState:UIControlStateDisabled]);
}
-(void)goToNew:(id)sender
{
UIButton *btnTemp = (UIButton *)[yourTableView viewWithTag:sender];
int iTag = btnTemp.tag;
}
I have one doubt that how to get other control's value when we select a row or tap on button while having custom tableViewCell.
Suppose I have a TableView Cell with a TextField, a Slider and button. Button has action using:
btn.tag = indexPath.row;
[btn addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
Now I can get button on its action method. I need to get textField's value and slider's value on button tap.
The one option is I create a array and store values for these two control in that array and manage this. But I don't think this is correct way to manage it.
Please navigate me to the currect way to get value of UITableViewCell's subview's value.
Thanks
While creating your textfield and slider, give them a constant tag and then add to cell's contentView
textfield.tag= TEXTFIELDTAG; //TEXTFIELDTAG = 100 or any other constant
[cell.contentView addSubview:textfield];
and then in your buttonTapped: method
-(void)buttonTapped:(id)sender
{
int row = ((UIButton*)sender).tag;
NSIndexPath* indexpath = [NSIndexPath indexPathForRow:row inSection:0]; // in case this row in in your first section
UITableViewCell* cell = [table cellForRowAtIndexPath:indexPath];
UITextField* textfield = [cell.contentView viewWithTag:TEXTFIELDTAG];
}
Whenever you create subviews (button , text field etc. ) for your UITableViewCell don't forget to tagged them.
myTextField.tag = 0;
myTextLabel.tag = 1;
[cell addSubview:myTextField];
[cell addSubview:myTextLabel];
Once you tagged, you could access them using below method.
- (UIView *)viewWithTag:(NSInteger)tag
When you select,you could get the subviews of your UITableViewCell's by using ViewWithTag function of UIView.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableview cellForRowAtIndexPath:indexPath];
UITextField* myTextField = [cell viewWithTag:0];
UILabel* myTextLabel = [cell viewWithTag:1];
}