2013-01-10 83 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; 
} 

我希望它可以帮助你。这个代码的想法不是我的。我也是在这里找到它的。不知道究竟在哪里。

相关问题