【问题标题】:Keep UITableViewCell accessory on top将 UITableViewCell 附件放在顶部
【发布时间】:2015-02-01 20:49:31
【问题描述】:

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

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

    return cell;
}

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

【问题讨论】:

    标签: ios objective-c uitableview accessorytype


    【解决方案1】:

    我的错误,实际上你不能对tableView的默认accessoryView做任何改变,你需要为你的cell创建一个自定义的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;
    }
    

    【讨论】:

    • 我试过了,但是没有用。我确实有一个自定义单元格,所以也许这就是原因?
    猜你喜欢
    • 1970-01-01
    • 2010-10-29
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多