2012-12-04 5 views
0

구조가 어떻게 보이는지입니다.사용자 정의 테이블 뷰 셀의 버튼이있는 행을 삭제합니다.

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

tableview에서 나는 사용자 정의 tableview 셀에 버튼이 있습니다. 여기 내 CellForRowAtIndex에있는 버튼의 코드는

[cell.btnDelete addTarget:self action:@selector(deleteAppointment:) forControlEvents:UIControlEventTouchUpInside]; 

이제 내 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); 

내가 이렇게 특정 행을 얻을하지만 여전히 다음과 같은 오류를주고있다.

-[ExceptionCell indexPathForCell:]: unrecognized selector sent to instance 0x210a2fa0 

아무도 도와 줄 수 있습니까?

+0

내 솔루션을 살펴 봐야합니다 – Marc

답변

6

정말 간단합니다. cellForRowAtIndexPath입니다.

cell.button.tag = indexPath.row; 

[array removeObjectAtIndex:button.tag]; 

지금있는 tableView를 다시로드 버튼 @selector 방법에 배열 또는 사전 (사용자의 데이터 소스를 먹으 렴)에서 값을 제거 같은 당신의 버튼을 태그. 의 TableView에서 행을 삭제할 때 당신이 원하는 무엇

[tableView reloadData]; 
+0

그 솔루션을 사용하면 TableView 메서드 DeleteRows가 제공하는 직관적 인 애니메이션을 사용할 수 없습니다! – Marc

+0

당신은 애니메이션을 가지고 그것을 시도하십시오. 먼저 애니메이션을 수행 한 다음 인덱스를 업데이트하기 위해 테이블을 다시로드합니다. – junaidsidhu

0

indexPathForCell을 호출하려고합니다. indexPathForCellUITableView에 대한 방법이며 UITableViewCell이 아닙니다.

cell.superview이 (가) 귀하의 UITableView을 (를) 반환하지 않습니다.

+0

예 내 tableview를 내 scrollview에 넣었 기 때문입니다. 이것이 가능한가 ? – Steaphann

0

이 방법 DeleteRowsAtIndexPath을 제공하는 직관적 인 애니메이션을 유지하는 것입니다, 그 문제를 해결하고, 그리고 마침내 쉬운 솔루션을 발견했다. 질문은 질문으로

, 우리가

그래서 셀 삭제에 대한 사용자 정의있는 UIButton와 사용자 정의 셀을 사용하고, 당신은 위임 // 콜백을 사용해야합니다. 다음 데모는 C#의 MonoTouch를위한 데모입니다. 객관적인 C에서와 동일하지만 메서드의 이름이 약간 다릅니다 + 구문입니다.

// 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 내 공장 방법

if (!(this.ItemDeleted is Delegate)) { 
      this.ItemDeleted += new CustomCell.FOODelegateMethods (ResultCallBack); 
     } 

과 다음의 deleteItem 작업에

public delegate void FOODelegateMethods (MyObject item, CustomCell instance); 
public event FOODelegateMethods ItemDeleted; 

partial void DeleteItem (NSObject sender) 
    { 
     ItemDeleted(arrayObject, this); 
    } 
관련 문제