2011-04-29 4 views
2

개체로 채워진 UITableView가 있습니다. didSelectRowAtIndexPath 메서드에서 행을 선택하면 UITableViewCellAccessoryCheckmark가 나타나고 선택하지 않으면 사라집니다.스크롤 할 때 UITableViewCellAccessory가 사라진다 화면

을 heres didSelectRowAtIndexPath 메서드에 대한 코드 :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
[tableView deselectRowAtIndexPath:indexPath animated:NO]; 
UITableViewCell *curCell = [beerTable cellForRowAtIndexPath:indexPath]; 

if (curCell.accessoryType == UITableViewCellAccessoryCheckmark) { 
    [curCell setAccessoryType:UITableViewCellAccessoryNone]; 
    compareCount = (compareCount - 1); 

    if (tableView == [[self searchDisplayController] searchResultsTableView]) { 
     NSString *objBeer = [searchResults objectAtIndex:indexPath.row]; 
     [compareBeers removeObject:[searchResults objectAtIndex:indexPath.row]]; 
     [compareCarbs removeObject:[carbAmount objectAtIndex:[beerNames indexOfObject:objBeer]]]; 
    } 
    else { 
     [compareBeers removeObject:[beerNames objectAtIndex:indexPath.row]]; 
     [compareCarbs removeObject:[carbAmount objectAtIndex:indexPath.row]]; 
    } 

} 
else { 
    [curCell setAccessoryType:UITableViewCellAccessoryCheckmark]; 
    compareCount = (compareCount + 1); 

    if (tableView == [[self searchDisplayController] searchResultsTableView]) { 
     NSString *objBeer = [searchResults objectAtIndex:indexPath.row]; 
     [compareBeers addObject:[searchResults objectAtIndex:indexPath.row]]; 
     [compareCarbs addObject:[carbAmount objectAtIndex:[beerNames indexOfObject:objBeer]]]; 
    } 
    else { 
     [compareBeers addObject:[beerNames objectAtIndex:indexPath.row]]; 
     [compareCarbs addObject:[carbAmount objectAtIndex:indexPath.row]]; 
    } 

} 

if (compareCount > 0) { 
    if (compareOn == YES){ 
    } 
    else { 
    compareButton.enabled = YES; 
    UIImage *image = [UIImage imageNamed:@"redbutton.png"]; 
    [compareButton setImage:image]; 
    } 
} 

else { 
    compareButton.enabled = NO; 
    [compareButton setImage:nil]; 
    [compareButton setCustomView:nil]; 
} 

} 

가 나는 또한 내 cellForIndexPath로 이것을 가지고가 :

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


static NSString *CellIdentifier = @"CustomCell"; 

CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 

    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 

    for (id currentObject in topLevelObjects){ 
     if ([currentObject isKindOfClass:[UITableViewCell class]]){ 
      cell = (CustomCell *) currentObject; 
      break; 
     } 
    } 
} 

// Setting separate tables correctly.... 


return cell; 
} 

가 내 문제가 선택된 셀이 스크롤된다 보지 못했을 때 다시 볼 때 그 값과 관련된 체크 표시가 사라졌습니다.

체크 표시가 사라지지 않게하려면 어떻게해야합니까? 당신이 스크롤 할 때

감사

답변

0

있는 UITableViewCell 재활용된다. 따라서 tableView를 아래 위로 스크롤하면 볼 수있는 셀이 이전에 표시된 셀과 다를 수 있습니다.

cellForRowAtIndexPath에서 매번 셀의 상태를 재설정해야합니다. 코드에이 주석 영역이 있습니다 // 별도의 테이블을 올바르게 설정하는 중 ....

여기가 설정입니다. 호출 될 때 셀에 체크 표시가 나타나는지 확인하고 적절하게 설정하십시오.

4

데이터를 스크롤 할 때 셀이 다시 사용됩니다 (dequeueReusableCellWithIdentifier의 역할). didSelectRowAtIndexPath에 체크 표시가있는 셀에는 다른 행에 대해 재활용되고 더 이상 확인 된 행에 연결되지 않습니다.

cellForRowAtIndexPath에서 액세서리보기를 설정/설정 해제해야 검사 된 행이 다시보기로 스크롤 될 때 적절하게 검사됩니다.

+0

사용자가 탭을 사용하여 행을 선택하면 cellForRowAtIndexPath에 체크 표시를 설정할 수 있습니까? 어떤 종류의 예를 들어 보겠습니까? –

+1

체크 표시를 설정하기 위해 행을 탭하면 해당 정보를 모델 또는 컨트롤러 자체에 저장해야합니다. 사용자가 행을 클릭 할 때 색인 경로로 사전에 검사 된 상태를 저장 한 다음 해당 값을 사용하여 cellForRowAtIndexPath에서 검사 된 상태를 설정하는 것과 같이 간단한 작업을 수행 할 수 있습니다. –

0

didSelectRow 메서드에서 선택한 셀의 배열 또는 사전에 선택된 인덱스를 저장하려고 시도하고 cellForRowAtIndexPath에서 해당 인덱스가 배열에서 사용 가능한지 확인한 다음 accesoryType을 설정하여 셀에 확인 표시를하십시오. 당신이 내가 가장 일을 찾을 것입니다

0

이해 희망 :

(단지 사이에 코드가 "이 추가"와 "이 추가 끝")는 "변경해야합니다, 또한

을 (전화 번호 ofRows) "를 int를 반환하는 객체 또는 int 자체에 반환합니다. 이러한 myCustomArray.count 첫째로, 또는보다 일반적인 접근을 원하는 사람 24.

//In .h file add this 
NSMutableArray *indexArray; 

//in .m file 

- (void)viewDidLoad { 
    //Add this 
    indexArray = [[NSMutableArray alloc] init]; 
    int i; 
    for (i = 0; i <= (Your Number ofRows); i++) { 
     [indexArray addObject:[NSNumber numberWithDouble:0]]; 
    } 
NSLog(@"indexArray = %@",indexArray);//You can check the console now to see if you have an array of "zeros" 
    //End Add this 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
[tableView deselectRowAtIndexPath:indexPath animated:NO]; 
UITableViewCell *curCell = [beerTable cellForRowAtIndexPath:indexPath]; 


if (curCell.accessoryType == UITableViewCellAccessoryCheckmark) { 
    [curCell setAccessoryType:UITableViewCellAccessoryNone]; 
    compareCount = (compareCount - 1); 

    //Add this 
    [indexArray replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithDouble:0]]; 
    //End Add this 


    if (tableView == [[self searchDisplayController] searchResultsTableView]) { 
     NSString *objBeer = [searchResults objectAtIndex:indexPath.row]; 
     [compareBeers removeObject:[searchResults objectAtIndex:indexPath.row]]; 
     [compareCarbs removeObject:[carbAmount objectAtIndex:[beerNames indexOfObject:objBeer]]]; 
    } 
    else { 
     [compareBeers removeObject:[beerNames objectAtIndex:indexPath.row]]; 
     [compareCarbs removeObject:[carbAmount objectAtIndex:indexPath.row]]; 
    } 

} 
else { 
    [curCell setAccessoryType:UITableViewCellAccessoryCheckmark]; 
    compareCount = (compareCount + 1); 

    //Add this 
    [indexArray replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithDouble:1]]; 
    //End Add this 

    if (tableView == [[self searchDisplayController] searchResultsTableView]) { 
     NSString *objBeer = [searchResults objectAtIndex:indexPath.row]; 
     [compareBeers addObject:[searchResults objectAtIndex:indexPath.row]]; 
     [compareCarbs addObject:[carbAmount objectAtIndex:[beerNames indexOfObject:objBeer]]]; 
    } 
    else { 
     [compareBeers addObject:[beerNames objectAtIndex:indexPath.row]]; 
     [compareCarbs addObject:[carbAmount objectAtIndex:indexPath.row]]; 
    } 

} 

if (compareCount > 0) { 
    if (compareOn == YES){ 
    } 
    else { 
     compareButton.enabled = YES; 
     UIImage *image = [UIImage imageNamed:@"redbutton.png"]; 
     [compareButton setImage:image]; 
    } 
} 

else { 
    compareButton.enabled = NO; 
    [compareButton setImage:nil]; 
    [compareButton setCustomView:nil]; 
} 

}  



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


static NSString *CellIdentifier = @"CustomCell"; 

CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 

    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 

    for (id currentObject in topLevelObjects){ 
     if ([currentObject isKindOfClass:[UITableViewCell class]]){ 
      cell = (CustomCell *) currentObject; 
      break; 
     } 
    } 
} 

// Setting separate tables correctly.... 

//Add this 
if ([[indexArray objectAtIndex:indexPath.row] doubleValue] == 1) { 
    cell.accesoryType = UITableViewCellAccesoryTypeCheckmark; 
} else { 
    cell.accesoryType = UITableViewCellAccesoryTypeNone; 
} 
//End Add this 

return cell; 
} 
1

복수 선택한 셀

위해 선택된 셀을 추적하는 (있는 NSMutableArray *) selectedCells를 생성한다. 다음은 체크 표시가 추가 셀을 선택 어느 때 지금이 대리자 메서드

-(void)tableView:(UITableView *)tableView 
    didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

//add the checkmark to cell when selected 
if ([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryNone){ 
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 

} 

//once selected add that selected cell to the selectedCells array 

[self.selectedCells addObject:@(indexPath.row) ]; 

} 

-(void)tableView:(UITableView *)tableView 
       didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{ 

//set the accessory type back to none 
if([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark){ 
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; 
} 

//remove the selected cells index from the selectedCells array 
[self.selectedCells removeObject:@(indexPath.row) ]; 

} 

를 구현하고 indexPath.row가있는 NSMutableArray에 저장된다. 해당 셀을 선택 취소하면 체크 표시가 제거되고 배열에서 제거됩니다. 배열은 검사 된 셀만 보유한다는 것을 의미합니다.

그런 다음이 배열을 사용하여 cellForRowAtIndexPath 메소드에서 올바른 accessoryType을 지정합니다. 이 메소드는 tableView에 셀이 필요할 때마다 호출되며, 셀이 생성 될 때마다 checkMark가 있어야합니다.

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

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 

//Create an NSNumber to hold the row of the cell to be created 
    NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row]; 

//then ask the array if the selectedCells array has that object. 
//if it does then that cell needs a checkmark when created. 

if ([self.selectedCells containsObject:rowNsNum]){ 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 
else { cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    [cell.textLabel setText:@"your contents"]; 
} 

return cell; 
} 
관련 문제