将UITableViewCell配件放在首位

时间:2015-02-01 20:49:31

标签: ios objective-c uitableview accessorytype

我有一个带有附件类型详细指示符的UITableViewCell。当我调整单元格的大小时,单元格附件会自动居中。我怎样才能让它保持领先,而不是居中?

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

    return cell;
}

如果可能的话,我更喜欢在故事板中这样做,如果没有,我很高兴听到如何以编程方式进行。

1 个答案:

答案 0 :(得分:0)

我的错误,实际上你无法对tableView的默认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;
}