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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.