2012-05-19 4 views
4

두 섹션이있는 테이블을 만들었습니다. 두 번째 섹션이 아닌 첫 번째 섹션에 삭제 버튼을 표시 할 때 앱을 원합니다. 다음 코드를 작성했습니다.한 섹션에서 행을 삭제하는 방법

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if (indexPath.section == 0) { 
Book_own *temp= (Book_own *)[self.books objectAtIndex:indexPath.row]; 

if (editingStyle == UITableViewCellEditingStyleDelete) { 
    // Delete the row from the data source 

    [books removeObjectAtIndex:indexPath.row]; 

    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

} 

else if (editingStyle == UITableViewCellEditingStyleInsert) { 
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
} 
} 
} 

하지만 편집 버튼을 누르면 두 섹션 모두에 삭제 버튼이 표시됩니다. 어떻게 방지하고 첫 번째 섹션에서 삭제합니까 ???

답변

1

이 시도 ...

-(UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { 

     if(indexPath.section == yourVariable) return UITableViewCellEditingStyleDelete; 

     else return UITableViewCellEditingStyleNone; 

    } 
+0

덕분에이 날 (didSelectRowAtIndexPath)을 수행 할 수 있습니까? –

+0

에 대한 –

+0

예 UITableViewCellEditingStyleNone에서만 재생합니다. 이 말은 편집 스타일 만 의미하지만 UITableViewSelectionStyleNone은 의미가 없습니다. 우리는 아무 것도 변경하지 않으며 doselectrowatindexpath를 실행하는 데 책임이 있습니다 –

1

'UITableViewCellEditingStyleNone'은 이미 구현 한 위임의 특정 섹션에 설정할 수 있습니다. 그냥 반환하십시오.

당신은 다음과 같은 코드로 얻을 수 있습니다 - cellForRowAtIndexPath에

: -이

dataSource
if(indexPath.section==0) 
{ 
cell.editing=NO; 
} 
+0

나는이 시도하지만 여전히 삭제 버튼을 보여 주지만, 귀하의 답변을 주셔서 감사합니다 당신의 도움이 –

1

YES 또는 NO를 반환 tableView:canEditRowAtIndexPath:을 구현할 수 있습니다.

1

당신은 특정 섹션 확인할 수 있습니다 경우 (indexPath.section == editSection) {

반환 UITableViewCellEditingStyleDelete;

}

else{ 

반환 UITableViewCellEditingStyleNone;

은}

관련 문제