2012-01-12 5 views
1

UILongPressGestureRecognizer 이벤트에서 셀의 데이터를 가져오고 저장하려고합니다. 내가 시도하고있는 것은 사용자가 길게 누른 다음 길게 누른 다음 대화 상자가 열리면 (거기에 3 개 이상의 버튼이 있음) 사용자가 특정 셀 데이터를 저장하거나 테이블에서 해당 셀을 삭제할 수있는 선택권을 갖게됩니다. 또는 다른 화면으로 이동합니다. 다음은 UILongPressGestureRecognizer에서 테이블 뷰 셀의 데이터 저장

나는이 목적을 위해 코드를 사용하고 있습니다 : 여기

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    UILongPressGestureRecognizer *pressRecongnizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tableCellPressed:)]; 
    pressRecongnizer.minimumPressDuration = 0.5f; 
    [cell addGestureRecognizer:pressRecongnizer]; 
    [pressRecongnizer release]; 
} 

if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){ 
    cell.textLabel.text = 
    [self.filteredListItems objectAtIndex:indexPath.row]; 
} 
else{ 
    cell.textLabel.text = 
    [self.groups objectAtIndex:indexPath.row]; 
} 

return cell;} 

- (void)tableCellPressed:(UILongPressGestureRecognizer *)recognizer{ 

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add to Favourites", @"Take to Map", @"Delete" ,nil] ; 
[alert show];} 

내가 내 coreData에 데이터를 저장할 수있는 방법을 알고 싶어요?

답변

2

UIGestureRecognizer에는 첨부 된보기를 나타내는보기 속성이 있습니다.

UITableViewCell *cell = (UITableViewCell *)[recognizer view]; 
NSString *text = cell.textLabel.text; 

제스처 인식기를 각 셀에 넣었으므로 위의 코드를 사용하여 특정 셀을 쉽게 가져올 수 있습니다.

사용자가 선택한 선택 항목이 별도의 방법으로 반영되므로 UIAlertDelegate 메서드를 구현하고 데이터를 일시적으로 어딘가에 저장해야합니다.

UIAlertView에서 사용자의 선택이 다른 방법으로 제공되기 때문에, 당신은 당신이 등 indexPath, 세포의 인스턴스 변수를 생성 여부 (셀에 대한 참조를 저장해야한다 : 편집

.. 당신 옆까지).

- (void)tableCellPressed:(UILongPressGestureRecognizer *)recognizer { 
    UITableViewCell *cell = (UITableViewCell *)[recognizer view]; 

    self.myText = cell.textLabel.text; 
    self.currentCellIndexPath = [self.tableView indexPathForCell:cell]; 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add to Favourites", @"Take to Map", @"Delete" ,nil] ; 
    [alert show]; 
} 

셀을 삭제하려면 먼저 데이터 소스에서 셀을 삭제해야합니다. 지금까지 대리인 메서드를 사용 중입니다.

if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:@"Delete"]) { 
    [myArray removeObjectAtIndex:self.currentCellIndexPath]; // in this example, I saved the reference to the cell using a property 

    // last line of example code 
} 

이제 두 가지 방법 중 하나로 테이블보기를 업데이트해야합니다. 당신이 전망이 가지고있는 좋은 삭제 애니메이션 테이블을 원한다면, 당신이 사용할 수있는,

[self.tableView reloadData]; 

을 또는 :

[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:self.currentCellIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
+0

@Kevin 낮은 –

+0

고맙습니다하지만, 한 부분의 당신도 즉시 호출하여 테이블 뷰를 새로 고칠 수 있습니다 사용자가 대화 상자에서 삭제 단추를 두드리는 경우 테이블에서 특정 셀을 삭제할 수있는 방법이 내 질문입니다. –

+0

아, 죄송합니다! 알지 못 했어. 편집 됨 =). –

관련 문제