将 UITableViewCell 配件放在顶部

Keep UITableViewCell accessory on top

我有一个带有 accessorytype 详细信息指示器的 UITableViewCell。当我调整单元格大小时,单元格附件会水平居中。我怎样才能让它保持在顶部,而不是居中?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    customCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];

    return cell;
}

如果可能的话,我更愿意在情节提要中完成它,否则,我很高兴听到如何以编程方式完成它。

我的错误,实际上你不能对 tableView 的默认 accessoryView 做任何更改,你需要为你的单元格创建一个自定义的 accessoryView。只需忽略附件类型,因为一旦您创建了自己的 accessoryView,它就不再重要了。

在 CellForRowAtIndexPath 中尝试此代码

if(!cell.accessoryView)
{
   UIImage *image = [UIImage imageNamed:@"image.png"];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    button.frame = frame;
    [button setBackgroundImage:image forState:UIControlStateNormal];

    //add target if necessary
    [button addTarget:self action:@selector(checkButtonTapped:event:)  forControlEvents:UIControlEventTouchUpInside];

    //apply customisations 
    button.backgroundColor = [UIColor clearColor];
    cell.accessoryView = button;
}