2012-12-02 34 views
8

này được làm đầu tôi trong :-)Tuỳ chỉnh UITableViewCell trong chế độ chỉnh sửa không di chuyển UILabels tôi

Tôi có một CoreData đầy đủ chức năng Dân cư UITableView bên trong một UIViewController và tôi đã triển khai thành công "Vuốt sang Xóa tùy chọn" (đó là dễ dàng) và tôi cũng có thể xóa các trường hợp đơn lẻ bằng nút chỉnh sửa nơi vòng tròn màu đỏ thingy xuất hiện.

Vấn đề của tôi là, và tôi nghĩ đó là vì tôi có một CustomCell, khi tôi nhấn nút chỉnh sửa, UILabels sẽ không di chuyển sang phải.

Tôi đã thử sử dụng -(void)layoutSubViews và một vài loại khác, nhưng không có tác dụng.

Tôi đã đăng mã của mình cho số cellForRowAtIndexPath. Đây là một phần của phần ghi chú trong ứng dụng của tôi. Mã này hoạt động, tôi chỉ cần biết Làm cách nào để di chuyển các nhãn khi tôi chuyển sang chế độ Chỉnh sửa ??

Cảm ơn bạn những lời khuyên và tư vấn :-)

- (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

Are nhãn của bạn thêm trực tiếp vào tế bào hoặc bạn đã thêm chúng cho 'contentView' của tế bào? Chúng phải nằm trong 'contentView' để chúng tự động điều chỉnh khi di chuyển đến chế độ chỉnh sửa hoặc trang trí ô khác được thêm vào. – rmaddy

+0

Tôi đã tạo một Custom Class mới và kéo chúng vào đó từ Storyboard - Tôi nghĩ điều đó có nghĩa là chúng trực tiếp trên ô phải không ?? –

+0

Tôi đã thêm mã cho ô tùy chỉnh của mình - Tôi chưa thêm bất kỳ thứ gì trong tệp .m - không cần thiết để cập nhật ngay bây giờ. –

Trả lời

8

Các giải pháp khác có thể sẽ làm việc nhưng cách này sẽ làm hoạt hình tự động cho bạn. MNCustomCell sẽ không bố cục lại chế độ xem tùy thuộc vào trạng thái hiện tại của ô, nhưng nếu bạn thêm nhãn của mình vào contentView của ô, nó sẽ hiển thị.

Ví dụ sau sẽ di chuyển nhãn để nó không ảnh hưởng đến nút xóa.

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

Tôi đã thêm mã mà bạn đã nói ở đầu (mã thứ 3 của mã) là địa điểm phù hợp? Nó vẫn không hoạt động. Nhưng trong TableView tôi vẫn đặt textlabels như tôi có quyền ?? –

+0

Lô thứ ba của mã? Tôi không chắc ý bạn là gì. – Fredrik

+0

Xin lỗi, nếu bạn nhìn vào câu hỏi ban đầu, tôi đã chia mã đã gửi của tôi thành 3 phân đoạn. cái thứ 3 là cái tôi đang đề cập đến. nó ở ngay dưới cùng của câu hỏi. –

1

Thực hiện các phương pháp sau đây trong lớp tế bào tùy chỉnh của bạn.

- (void)willTransitionToState:(UITableViewCellStateMask)state 

- (void)didTransitionToState:(UITableViewCellStateMask)state 

và di chuyển nhãn của bạn cho phù hợp.

Nó phải là như

- (void)willTransitionToState:(UITableViewCellStateMask)state { 

    [super willTransitionToState:state]; 

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

Edit:

- (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

Bạn phải thay đổi vị trí khung trong cả hai phương pháp. Trong phương thức 'will..', bạn phải di chuyển nhãn phải, và trong phương thức' did..', bạn phải di chuyển nhãn sang trái. – Ilanchezhian

+0

ok - Tôi đã làm việc đó, tuy nhiên nhãn không hoạt ảnh với nó, nó chỉ chuyển sang vị trí mới. Tôi đã thử [UIView beginAnimations: nil context: NULL]; nhưng có nhiều sự chậm trễ để làm cho nó trông tự nhiên. –

+0

Xem câu trả lời đã chỉnh sửa của tôi – Ilanchezhian

Các vấn đề liên quan