2012-12-02 132 views
8

這是做我的頭:-)自定義的UITableViewCell在編輯模式

不動我的UILabels我有一個功能齊全CoreData填充的UITableView一個UIViewController內,我已經成功地實施了「滑動刪除選項」(這很簡單),我也可以用一個編輯按鈕來刪除單個實例,在那裏出現紅圈。

我的問題是,我認爲這是因爲我有一個CustomCell,當我按下編輯按鈕UILabels不會移動到右側。

我一直在使用別人-(void)layoutSubViews和一些嘗試,但沒有任何工程。

我已經發布我的代碼爲我cellForRowAtIndexPath。這是我的應用中註釋部分的一部分。此代碼有效,我只需要知道如何在進入編輯模式時移動標籤?

謝謝你的提示和建議:-)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *cellIdentifier = @"Cell"; 

MNCustomCell *cell = [_mainTableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; 
if (cell == nil) { 
    cell = [[MNCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
} 


//cell.textLabel.text = [_tableArray objectAtIndex:indexPath.row]; 

MNotes *mnotes = [[self fetchedResultsController] objectAtIndexPath:indexPath]; 

cell.noteTitle.text = mnotes.noteTitleString; 
cell.noteSummary.text = mnotes.mainNoteString; 

mnotes.createDate = [[NSDate alloc] init]; 
SORelativeDateTransformer *relativeDateTransformer = [[SORelativeDateTransformer alloc] init]; 
NSString *relativeDate = [relativeDateTransformer transformedValue:mnotes.createDate]; 

cell.noteDate.text = relativeDate; 


cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

return cell; 
} 

-

//This is the Custom Cell 
@interface MNCustomCell : UITableViewCell 
{ 

} 

@property (strong, nonatomic) IBOutlet UILabel *noteTitle; 
@property (strong, nonatomic) IBOutlet UILabel *noteDate; 

@property (strong, nonatomic) IBOutlet UITextView *noteSummary; 


@end 

-

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

    [self.contentView addSubview:_noteTitle]; 
    [self.contentView addSubview:_noteSummary]; 
    [self.contentView addSubview:_noteDate]; 

    } 
    return self; 
    } 
+0

是你的標籤直接添加到細胞還是你將它們添加到單元格的'contentView'?他們應該在'contentView'中讓他們在單元格進入編輯模式或添加其他單元格裝飾時自動進行調整。 – rmaddy

+0

我創建了一個新的自定義類並將它們拖入Storyboard中 - 我認爲這意味着它們直接位於單元格右側? –

+0

我已經添加了我的自定義單元的代碼 - 我沒有在.m文件中添加任何內容 - 現在不需要直到現在。 –

回答

8

另一種解決方案可能會工作,但這種方法會爲你自動做動畫。 MNCustomCell不會根據單元格的當前狀態重新佈置視圖,但是如果將標籤添加到單元格的contentView中,它將會。

下面的例子將移動標籤,所以它不會刪除按鈕干擾。

MNCustomCell.m

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 220.0, 15.0)]]; 
     mainLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight; 
     [cell.contentView addSubview:mainLabel]; 
+0

我已經添加了你在頂部(第三批代碼)所說的代碼是否是正確的地方?它仍然不起作用。但在TableView中,我仍然設置文本標籤,因爲我有權利? –

+0

第三批代碼?我不確定你是什麼意思。 – Fredrik

+0

對不起,如果你看看原來的問題,我已經將我提交的代碼分成了三部分。第三個是我指的那個。這是正確的問題的底部。 –

1

實現您的自定義單元格級以下的方法。

- (void)willTransitionToState:(UITableViewCellStateMask)state 

- (void)didTransitionToState:(UITableViewCellStateMask)state 

,並相應地移動你的標籤。

應該是這樣

- (void)willTransitionToState:(UITableViewCellStateMask)state { 

    [super willTransitionToState:state]; 

    if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask) { 
     label.frame = ... 
    } 
} 

編輯:

- (void)willTransitionToState:(UITableViewCellStateMask)state { 

    [super willTransitionToState:state]; 

// label.hidden = YES; 
// label.alpha = 0.0; 
} 

- (void)didTransitionToState:(UITableViewCellStateMask)state { 

    [super didTransitionToState:state]; 

    if (state == UITableViewCellStateShowingDeleteConfirmationMask) { 

     [UIView beginAnimations:@"anim" context:nil]; 
     label.frame = leftFrame; 
     [UIView commitAnimations]; 
    } else if (state == UITableViewCellStateDefaultMask) { 

     [UIView beginAnimations:@"anim" context:nil]; 
     label.frame = rightFrame; 
     [UIView commitAnimations]; 
    } 
} 
+0

您必須更改兩種方法中的框架位置。在'will..'方法中,您必須將標籤右移,並使用'did..'方法,您必須將標籤左移。 – Ilanchezhian

+0

好的 - 我得到它的工作,但標籤不與它生成動畫,它只是輕彈到新的位置。我試過[UIView beginAnimations:nil context:NULL];但有很多延遲讓它看起來很自然。 –

+0

看到我編輯的答案 – Ilanchezhian

相關問題