ios - 为什么我的附件按钮会向左移动以获得更高的表格单元格?

标签 ios uitableview

我的 iOS 应用程序中有一个表格,其中包含三个表格单元格,每个表格单元格都有一个自定义附件按钮。其中一个单元格需要比其他单元格高;它是 60px 而不是 45px。在这种情况下,附件按钮会移动到左侧,而如果它们的高度都相同,则附件按钮会排成一行。

辅助按钮是由相同的代码创建的,因此它们应该是相同的。这个问题似乎与 UITableViewCell 本身有关。

它最终看起来像这样。我没有在屏幕抓取中包含上边框,但上面的单元格更高。有谁知道我该如何解决这个问题?

Misaligned accessory views:

下面是如何创建单元格的示例。这些仅在名称上有所不同;高度由 tableView 指定:heightForRowAtIndexPath:

    cell = [[UITableViewCell alloc] init];
    label = [[UILabel alloc] initWithFrame:[cell frame]];
    [label setText:@"Favorites"];
    [cell.contentView addSubview:label];
    [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    image = [UIImage imageNamed:@"GT.png"];
    [button setBackgroundImage:image forState:UIControlStateNormal];
    button.frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    [cell setAccessoryView:button];

最佳答案

您正在设置附件 View 和附件类型。 做一个或另一个。我会摆脱 setAccessoryView:button

[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
[cell setAccessoryView:button];

另外,你为什么要这样做:

cell = [[UITableViewCell alloc] init];

你应该在 cellForRowAtIndexPath 中创建你的单元格,你应该有这样的东西:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = nil;
    if (cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = @"Some Text";
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

关于ios - 为什么我的附件按钮会向左移动以获得更高的表格单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11513670/

相关文章:

iphone - 每个部分具有最大行数的可重新排序的 UITableView

objective-c - UItableViewCell 与 UIDatePicker : month column gets covered

ios - 如何在 objective-c 中导航到新 Storyboard文件上的 View Controller

ios - 我怎样才能加快 UITableView 的速度?

iOS 8 - 在启用 AutoLayout 的情况下更改 UIScrollView 的框架

ios - 防止 TableView 在 insertRows 后滚动到顶部

ios - 获取uitableview的多个选定行信息

ios - 向 uitableviewcell 添加文本或绘图

ios - 如何在 Swift 中拆分 JSON 字符串?

ios - 从 NSDate 生成格式化的日期时间字符串