2013-12-09 3 views
0

플레이어 6에서 플레이어 6까지 6 행이있는 테이블 뷰가 있습니다. 사용자는 플레이어 이름을 입력하고 스 와이프하고 '삭제'를 눌러 행을 삭제할 수 있습니다. 행의 텍스트 필드에 텍스트가있을 때 문제가 발생합니다. 예를 들어 처음 3 행의 텍스트 필드에 'One'을 'Three'로 채 웁니다. '3'이라는 행 3을 삭제하면 행이 삭제되지만 텍스트 필드의 'Three'텍스트는 플레이어 4의 행 아래로 이동합니다. 이 문제를 해결하는 가장 좋은 방법은 무엇입니까?UITableView에서 행을 삭제할 때 UITextField 내용을 삭제합니다.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"playerCell"; 
    playerCell *cell = [tableView 
          dequeueReusableCellWithIdentifier:CellIdentifier 
          forIndexPath:indexPath]; 

    cell.playerLabel.text = [[_playerNames objectAtIndex:indexPath.row]objectForKey:@"title"]; 
    NSString *test = [NSString stringWithFormat:@"%@", cell.playerNameBox.text]; 
    cell.playerNameBox.tag = indexPath.row; 

    NSString *key = [NSString stringWithFormat:@"%ld", (long)indexPath.row]; 
    [[NSUserDefaults standardUserDefaults] 
    setObject:test forKey:key]; 

    return cell; 
} 

행 삭제 : 섹션의 행

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     [_playerNames removeObjectAtIndex:indexPath.row]; 
     [Table reloadData]; 

    } 
} 

수 = 당신의 "cellForRowAtIndexPath"방법에서 6

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return _playerNames.count; 
} 
+0

셀에 대한 사용자 정의 클래스가 있다고 가정하면'prepareForReuse' 메소드에서 텍스트 필드를'nil'으로 설정하십시오. – bobnoble

+0

이렇게하면 모든 텍스트 필드의 텍스트가 삭제됩니다. 해당 행의 텍스트 필드에서 텍스트를 삭제하는 방법이 있습니까? –

답변

1

을, 당신은 전무 객체의 경우 제대로 확인하지 않는 .

cell.playerLabel.text = [[_playerNames objectAtIndex:indexPath.row]objectForKey:@"title"]; 

해야 할 것 : 대신의

NSString *title = [[_playerNames objectAtInex:indexPath.row] objectForKey:@"title]; 
cell.playerLabel.text = (title ? title : @""); // set the label to either title or the empty string 

내가 무슨 일이 일어나고 있는지 의심이 제대로 다시로드 또는 재사용에 텍스트 레이블을 재설정하지 않을 것입니다.

어쨌든, 데이터를 다시로드하는 대신, 어색하지 말고 삭제 표시가 된 행을 삭제하십시오.

즉 : 또 다른 참고

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     [_playerNames removeObjectAtIndex:indexPath.row]; 
     [tableView deleteRowsAtIndexPaths: [NSArray arrayWithObject: indexPath] withRowAnimation: UITableViewRowAnimationLeft]; 
    } 
} 

: 난 당신이 한주의

: 코드에서

[Table reloadData]; 

. Objective-C의 모범 사례는 이 아니며은 대문자로 인스턴스 변수를 명명해야합니다. 'itemTable'과 같이 좀 더 기술적 인 내용이어야합니다.

+0

그게 효과가있다. 필자는 테이블 뷰를 사용한 적이 없기 때문에 여전히 어떻게 작동 하는지를 알고 있습니다. 내 인스턴스 변수의 이름을 바꾸는 과정. –

관련 문제