2013-01-10 3 views
0

사용자 정의 셀에 UITableView이 있습니다. 각 셀에는 ImageView과 세 개의 레이블이 있으며 문자열 배열에서 데이터를 가져옵니다. 스토리 보드에서 레이아웃을 완료했습니다. 데이터 소스는 문자열 배열입니다. 이 작동합니다.UITableView 셀 내용이 편집 중에 움직이지 않습니다.

이제 코드에 EditButton을 삽입했습니다. 이제 EditButton을 볼 수 있지만 편집 모드를 활성화하면 표 셀의 크기가 조정되지만 이미지와 레이블은 이동하지 않습니다.

셀의 내용을 이동하는 방법을 보여 줄 수 있습니까? UITableView 자습서를 아는 사람은 EditMode 및 스토리 보드를 사용합니다. 내가 찾은 모든 자습서는 "구형"Xcode를 기반으로합니다. 모든

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    myData = [NSMutableArray arrayWithObjects: 
       @"Line1_Label1|Line1_Label2|Line1_Label3", 
       @"Line2_Label1|Line2_Label2|Line2_Label3", 
       @"Line3_Label1|Line3_Label2|Line3_Label3", 
       nil]; 
    self.navigationItem.leftBarButtonItem = self.editButtonItem; 
} 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [myData count]; 
} 

// Return a cell for the table 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // A cell identifier which matches our identifier in IB 
    static NSString *CellIdentifier = @"CellIdentifier"; 

    // Create or reuse a cell 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Get the cell label using its tag and set it 
    NSString *currentItem = [myData objectAtIndex:indexPath.row]; 
    NSArray *itemArray = [currentItem componentsSeparatedByString:@"|"]; 

    UILabel *cellLabel = (UILabel *)[cell viewWithTag:1]; 
    [cellLabel setText:itemArray[0]]; 

    UILabel *cellLabel2 = (UILabel *)[cell viewWithTag:3]; 
    [cellLabel2 setText:itemArray[1]]; 

    UILabel *cellLabel3 = (UILabel *)[cell viewWithTag:4]; 
    [cellLabel3 setText:itemArray[2]]; 

    // get the cell imageview using its tag and set it 
    UIImageView *cellImage = (UIImageView *)[cell viewWithTag:2]; 
    [cellImage setImage:[UIImage imageNamed: @"control.png"]]; 

    return cell; 
} 

// Do some customisation of our new view when a table item has been selected 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    // Make sure we're referring to the correct segue 
    if ([[segue identifier] isEqualToString:@"ShowSelectedMovie"]) { 

     // Get reference to the destination view controller 
     ItemViewController *vc = [segue destinationViewController]; 

     // get the selected index 
     NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row]; 

     // Pass the name and index of our film 
     [vc setSelectedItem:[NSString stringWithFormat:@"%@", [myData objectAtIndex:selectedIndex]]]; 
     [vc setSelectedIndex:selectedIndex]; 
    } 
} 

@end 

답변

0

첫째, .H에있는 tableview의 함께 IBOutlet을하고하는 .m에 합성 :

여기 내 코드입니다, 그런데 당신에게

대단히 감사합니다.

그런 다음 편집 단추에 조치를 취하십시오 (없는 경우).

CGRect rect = yourTableView.cell.contentView.frame; 
    //Do whatever changes you wish to do with the sizing of the view. origin changes placement and size changes size (duh). Line below is an example. 
    rect.origin.y = yourTableView.cell.contentView.frame.origin.y - 20; 

    yourTableView.cell.contentView.frame = rect; 

이이 애니메이션되지 않습니다,하지만 난 그것이 당신의 목적을 성취 것 같아요 : 작업에 쓰기.

-1

사용자 정의 UITableViewCellController.m의 -(void)layoutSubviews{} - 메소드를 덮어 쓰거나 사용자 정의 UITableViewCellController를 사용하지 않는 경우 UITableViewController에서 시도하십시오. 하지만 아직 사용자 지정 UITableViewCellController 그것을 시도하지 않았습니다.

-(void) layoutSubviews { 
     [super layoutSubviews]; 
     CGFloat xPositionOfElementInTableCell = 273.0f; /* the position of the element before going into edit mode */ 

     if (self.isEditing && !self.showingDeleteConfirmation) // if we enter editing mode but not tapped on the red minus at the moment 
     { 
      xPositionOfElementInTableCell = 241.0f; 
     } else if (self.isEditing && self.showingDeleteConfirmation) // after we tappet on the red minus 
      xPositionOfElement = 193.0f; 
     } 


     CGRect frameOfElementInTableCell = self.myElementInTableCell.frame; 
     frameOfElementInTableCell.origin.x = xPositionofElement; 
     self.myElementInTableCell.frame = frameOfElementInTableCell; 
} 

내가 당신을 도움이되기를 바랍니다 :이 같은

뭔가 트릭을 할 것입니다. 이 코드의 아이디어는 내 것이 아닙니다. 나는 그것을 여기에서 찾아 냈다. 정확히 어디에서 왔는지 모릅니다.

관련 문제