0

私はUITableViewいくつかのカスタムセルを持っています。各セルにはImageViewと 3 つのラベルがあり、文字列配列からデータを取得します。ストーリーボードでレイアウトを行いました。データ ソースは文字列配列です。これは機能します。

EditButtonこれで、コードにa が挿入されました。が表示EditButtonされるようになりましたが、編集モードをアクティブにすると、表のセルのサイズが変更されますが、画像とラベルは移動しません。

セルの内容を移動する方法を教えてください。UITableViewEditMode とストーリーボードを使用したチュートリアルを知っている人はいません。私が見つけたすべてのチュートリアルは、「古い」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
4

2 に答える 2

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;

これはアニメーション化されませんが、目的は果たせると思います。

于 2013-01-10T10:18:08.860 に答える
-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;
 }

お役に立てば幸いです。このコードのアイデアは私のものではありません。私もここSOで見つけました。正確にはどこかわからない。

于 2013-03-07T13:27:44.437 に答える