我的 iOS 应用程序中有一个表格,其中包含三个表格单元格,每个表格单元格都有一个自定义附件按钮。其中一个单元格需要比其他单元格高;它是 60px 而不是 45px。在这种情况下,附件按钮会移动到左侧,而如果它们的高度都相同,则附件按钮会排成一行。
辅助按钮是由相同的代码创建的,因此它们应该是相同的。这个问题似乎与 UITableViewCell 本身有关。
它最终看起来像这样。我没有在屏幕抓取中包含上边框,但上面的单元格更高。有谁知道我该如何解决这个问题?
下面是如何创建单元格的示例。这些仅在名称上有所不同;高度由 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/