2013-01-10 80 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

覆蓋-(void)layoutSubviews{} - 您自定義的UITableViewCellController.m的方法,或者如果您不使用自定義的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; 
} 

我希望它可以幫助你。這個代碼的想法不是我的。我也是在這裏找到它的。不知道究竟在哪裏。

相關問題