2012-03-07 10 views
3

UITableViewCellのラベルを非表示にして、ユーザーがスワイプしてセルを削除するたびにタイトルと重ならないようにしたい。私が使用して、細胞内のラベルを割り当てた削除するためにスワイプしたときにTableViewCellのラベルを非表示にする

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 

    if (editingStyle == UITableViewCellEditingStyleDelete) { 

     [self.tableView beginUpdates]; // Avoid NSInternalInconsistencyException 

     // Delete the project object that was swiped 
     Project *projectToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
     NSLog(@"Deleting (%@)", projectToDelete.name); 
     [self.managedObjectContext deleteObject:projectToDelete]; 
     [self.managedObjectContext save:nil]; 

     // Delete the (now empty) row on the table 
     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; 
     [self performFetch]; 

     [self.tableView endUpdates]; 
    } 
} 

を::

UILabel *projectDate = (UILabel *)[cell viewWithTag:3]; 
    projectDate.text = project.dateStarted; 

を、設定だけで試してみましたが、私は削除するスワイプを開始し、処理するために、次のコードを使用してい

projectDate.hidden = YES; 

ただし、これは機能しません。

答えて

6

これを実装するには、UITableViewCellをサブクラス化する必要があると思います。サブクラスでは- (void) setEditing:(BOOL)editing animated:(BOOL)animatedをオーバーライドします。この方法では、ラベルを非表示にすることができます。削除操作のラベルを非表示にする必要がある場合は、self.editingStyleを使用して、編集スタイル(別名:UITableViewCellEditingStyleDelete)に応じて条件付きでラベルを非表示にします。

ここに2つの例があります。私は例2を好む、それは簡単です。たとえば、テキストを置き換えると便利な場合があります。

@implementation CellSubclass{ 
    NSString *_labelText; //only used in example 1 
} 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated{ 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 
// Example 1, replacing the text value 
- (void) setEditing:(BOOL)editing animated:(BOOL)animated{ 
    [super setEditing:editing animated:animated]; 
    if (editing && self.editingStyle == UITableViewCellEditingStyleDelete){ 
     UILabel *label = (UILabel *)[self viewWithTag:3]; 
     _labelText = label.text; 
     self.textLabel.text = nil; 
    } else if (!editing && _labelText){ 
     UILabel *label = (UILabel *)[self viewWithTag:3]; 
     label.text = _labelText; 
    } 
} 

//Example 2 - hiding the view itself 
- (void) setEditing:(BOOL)editing animated:(BOOL)animated{ 
    [super setEditing:editing animated:animated]; 
    if (editing && self.editingStyle == UITableViewCellEditingStyleDelete){ 
     [self viewWithTag:3].alpha = 0.0f; 
    } else { 
     [self viewWithTag:3].alpha = 1.0f; 
    } 
} 

@end 

同じ名前のメソッドが2つありますのでご注意ください。それは明らかに大きなノーではありません....それらのうちの1つだけを使用してください。

また、私はアニメーションパラメータを無視しました。

 [UIView animateWithDuration:.3f animations:^{ 
      [self viewWithTag:3].alpha = 0.0f; 
     }]; 

:あなたは(別名...フェードイン/消えていく)、ラベルの消失は、第二の例でアニメーションさせたい場合はすべてを行う必要があるので、同じように、アニメーションブロックで変更を囲むです私はあなたが最初の例をアニメーション化できるとは思わない。

+0

こんにちは、私はUITableViewCellをサブクラス化するという概念を理解していますが、小さな例を提供できますか? – jcrowson

+0

ええ、私の答えを編集します...分。さて、答えは編集されました。 –

+1

いい仕事です(残念ながら2回upvoteすることはできません)が、あなたの例には微調整が必​​要です。setEditingのスーパーインプリメンテーションを呼び出す必要があります。また、 'animated'パラメータも考慮する必要があります。ラベルの消滅をアニメーション化する。 – jrturton

関連する問題