首页 文章

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

提问于
浏览
1

我的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];

1 回答

  • 1

    您正在设置附件视图和附件类型 . Do one or the other. 我会摆脱 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;
    }
    

相关问题