UITableView CustomCell - iphone

I want to do some operation in a particular cell using some function.
Here i used button in custom cell to call this function.
Each button has cell index value as title.
Using that title only, I can identify a particular cell to do some operation.
-(void)showInteractionView:(id)sender{
UIButton *btn=(UIButton *)sender;
lastSelInteractionVal=[btn.titleLabel.text intValue];
NSIndexPath *index=[NSIndexPath indexPathForRow:[btn.titleLabel.text intValue] inSection:0];
UITableViewCell *cell=[tblView cellForRowAtIndexPath:index];
UIView *v=(UIView *)[cell.contentView viewWithTag:INNERVIEW];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
v.frame=CGRectMake(0, 0, 320, 100);
[UIView commitAnimations];
}
But here I used custom cell so I want to assign tblView cell to a custom cell instead of this code below.
UITableViewCell *cell=[tblView cellForRowAtIndexPath:index];
Please give me a solution.
Thanks.

The same way you can get custom cell.
YourCustomCell *customCell = [tblView cellForRowAtIndexPath:index];
or you can do this
id Cell = [tblView cellForRowAtIndexPath:index];
if([cell isKindOfClass[yourCustomCellClassName class]]){
NSLog(#"Custom Cell Found");
}

Related

swipe to delete action replaced by button [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Replace action of swipe to delete by clicking on a button
I have the following code.
- (void) tableView: (UITableView *) tableView commitEditingStyle: (UITableViewCellEditingStyle) editingStyle forRowAtIndexPath: (NSIndexPath *) indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[appointments removeObjectAtIndex: indexPath.row]; // manipulate your data structure.
[tableView deleteRowsAtIndexPaths: [NSArray arrayWithObject: indexPath]
withRowAnimation: UITableViewRowAnimationFade];
NSLog(#"row deleted"); // Do whatever other UI updating you need to do.
}
}
This piece of code is going to be execute when I swipe on the cell. But what I want is that this code executes when I press a button in my custom tableview cell. Like you can see on the screenshot below I have 2 buttons on my tableview.
When the user presses the 'X' button the delete button should roll out like when you swipe the cell. I have the following action attached to my cell.
-(IBAction) deleteAppointment:(id) sender{
//show swipe delete button
}
And attached it in my cellForRowAtIndex at the following way.
cell.deleteAppointment.tag = indexPath.row;
[cell.deleteAppointment addTarget:self action:#selector(deleteAppointment:) forControlEvents:UIControlEventTouchUpInside];
just addTarget with button in your cell and set tag to every button like bellow..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
/////your code
UIButton *btnClose = [[UIButton alloc]initWithFrame:CGRectMake(270, 7, 20, 20)];
btnClose.tag = indexPath.row;
[btnClose setImage:[UIImage imageNamed:#"closeImage.png"] forState:UIControlStateNormal];
[btnClose addTarget:self action:#selector(deleteAppointment:) forControlEvents:UIControlStateNormal];
[cell addSubview:btnClose];
return cell;
}
and in the method see like this...
- (IBAction)deleteAppointment:(id)sender {
// here bellow code for swipe the close button in cell
UIButton *btn = (UIButton *)sender;
int SelectedRowNo = btn.tag;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[btn setFrame:CGRectMake(btn.frame.origin.x - 50, btn.frame.origin.y, btn.frame.size.width, btn.frame.size.height)];
[UIView commitAnimations];
//// here bellow code for delete raw from table
/*
[appointments removeObjectAtIndex: SelectedRowNo];
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *clickedButtonPath = [yourTableView indexPathForCell:clickedCell];
[yourTableView deleteRowsAtIndexPaths: [NSArray arrayWithObject: clickedButtonPath]
withRowAnimation: UITableViewRowAnimationFade];
NSLog(#"row deleted"); // Do whatever other UI updating you need to do.
*/
}
i hope this help you..

delete row with button in custom tableview cell

This is how the structure looks like.
--- UIView
---- ScrollView
--- TableView
.
UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, -250, 320, 250)];
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, 320, 150) style:UITableViewStylePlain];
tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
tableView.delegate = self;
tableView.dataSource = self;
tableView.backgroundColor = [UIColor blackColor];
tableView.separatorStyle = normal;
[tableView reloadData];
[topView addSubview:tableView];
[self.scrollView addSubview:topView];
In the tableview I am working with a custom tableview cell with a button in it. Here is the code for the button in my CellForRowAtIndex
[cell.btnDelete addTarget:self action:#selector(deleteAppointment:) forControlEvents:UIControlEventTouchUpInside];
Now to get the specific row I do this in my deleteAppointment
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *)button.superview;
UITableView *tableView = (UITableView *)cell.superview;
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
NSLog(#"row: %d",indexPath.row);
But it is still giving the following error.
-[ExceptionCell indexPathForCell:]: unrecognized selector sent to instance 0x210a2fa0
Can anyone help me?
its really Simple, in cellForRowAtIndexPath. just tag your button like
cell.button.tag = indexPath.row;
remove the value from array or dictionary (thats your dataSource) in button #selector method
[array removeObjectAtIndex:button.tag];
Now reload the tableView.
[tableView reloadData];
The error says you are trying to call indexPathForCell on ExceptionCell. indexPathForCell is a method on UITableView, not UITableViewCell.
cell.superview is not returning your UITableView as you expect.
What you want when deleting a Row from a TableView is to keep the Intuitive Animation that gives the method DeleteRowsAtIndexPath, I have been around that problem, and finaly found an EASY solution.
As the question asked, we are using a Custom Cell with a Custom UIButton for Deleting the Cell
So, you'll have to use delegate // CallBack.
That following demonstration is for MonoTouch in C#, it's the same in objective C, but methods are named a little bit differently + syntax.
//TableViewController
//CALLBACK Methods WHEN DELETING A ROW
public void DeletedItem (MyObject arrayObject, CustomCell cellInstance)
{
NSIndexPath[] arrayPath = new NSIndexPath[1] { this.myTableView.IndexPathForCell (cellInstance) };
this.myArray.RemoveItem (arrayObject);
this.myTableView.DeleteRows (arrayPath, UITableViewRowAnimation.Fade);
}
[Export("tableView:cellForRowAtIndexPath:")]
public virtual UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
...
cell.FactoryMethodsThatImUsing (myObject.items[indexPath.Row], DeletedItem);
return cell;
}
//CustomCell
public delegate void FOODelegateMethods (MyObject item, CustomCell instance);
public event FOODelegateMethods ItemDeleted;
IN MY Factory Method
if (!(this.ItemDeleted is Delegate)) {
this.ItemDeleted += new CustomCell.FOODelegateMethods (ResultCallBack);
}
And then on the DeleteItem Actions
partial void DeleteItem (NSObject sender)
{
ItemDeleted(arrayObject, this);
}

How to get other control value from UITableViewCell?

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

adjusting UITableView's cell at runtime

I have subclassed a UITableViewCell. What I want to have is when I click on the cell, I added a UIView to the bottom of the cell and adjust the height of the cell accordingly. Here's my code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCell* cell = (MyCell *) [self.table cellForRowAtIndexPath:indexPath];
UIView * options = [[UIView alloc] initWithFrame:CGRectMake(0, cell.frame.size.height - 27, cell.frame.size.width, 27)];
UIButton* button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[options addSubview:button1];
UIButton* button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[options addSubview:button2];
UIButton* button3 = [UIButton buttonWithType:UIButtonTypeCustom];
[options addSubview:button3];
//did some layout calculation here to position the button
[cell addSubview:options];
}
Here's a video that illustrates the issue
Also I've tried to change the accessoryView of my cell from my custom UIButton to the regular UITableViewCellAccessoryDisclosure but it didn't work
MyCell * cell = (MyCell *)[self.table cellForRowAtIndexPath:temp];
[cell.accessoryView removeFromSuperview];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
If you're curious what I am trying to do, I am trying to create a tweetbot uitableviewcell, where you press the cell/row and it presents you with other options (retweets, etc) and also it was shown animated.
I don't know if I correctly understand what you are trying to do. But if you do want to animate the change of the UITableViewCell's height try to adjust it within - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath and call
[tableView beginUpdates];
[tableView endUpdates];
Put controls into another cell and use
insertRowsAtIndexPaths:withRowAnimation:
to show it and
deleteRowsAtIndexPaths:withRowAnimation:
to hide it.

Cell specific Action like slide-in menu

I'm attempting to implement a slide-in menu like in the facebook app.
I have a NIB for a custom UITableViewCell, which includes a button.
The button has an IBAction associated with it, in which I animate in a subview of my NIB (the delete/edit menu).
My problem is that my animation only happens on one cell, and its not the cell where I pushed the button.
How would I call this animation to work on the cell where my button was tapped?
Here's my code so far. Any tips?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *PatientCellIdentifier = #"PatientCellIdentifier";
PatientListTableViewCell *cell = (PatientListTableViewCell *)[tableView dequeueReusableCellWithIdentifier:PatientCellIdentifier];
if (cell ==nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"PatientCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cellBG.png"]];
}
//NSUInteger row = [indexPath row];
//NSDictionary *rowData = [self.
[self configureCell:cell atIndexPath:indexPath];
return cell; }
-(IBAction)showMenu:(id)sender{
[self showMenuForCell:sender animated:YES];}
- (void)showMenuForCell:(UITableViewCell *)cell animated:(BOOL)animated {
//[super showMenu:view forCell:cell animated:animated];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
//Action here
[UIView commitAnimations];
}
Is this included in your code?
[self.contentView addSubview:SLIDINGVIEW];
We need to look at your animation block and the code that invokes your animation
I solved this problem Myself. I ended up using a custom Subclass of UITableViewCell outlined in 'Beginning iPhone Development' pp 205-208.
I put a button in the NIB for my cell, with one view for the normal view, and another for the menu. When the button was pushed. The main view slides out of the way.
All of these methods and buttons were included in the subClass.
Boom. done.