【问题标题】:UITableView content of cell dont move on editing单元格的 UITableView 内容不会继续编辑
【发布时间】:2013-01-10 09:16:05
【问题描述】:

我有一个带有一些自定义单元格的UITableView。在每个单元格中,有一个ImageView 和三个标签,并从字符串数组中获取数据。我已经在我的故事板中完成了布局。数据源是一个字符串数组。这行得通。

现在我在代码中插入了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

【问题讨论】:

    标签: objective-c xcode uitableview editmode


    【解决方案1】:

    首先在.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;
    

    这不会是动画,但我认为它会实现你的目的。

    【讨论】:

      【解决方案2】:

      覆盖-(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;
       }
      

      希望对你有帮助。这段代码的想法不是我的。我也在这里找到了它。不知道具体在哪里。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多