iOS UITableView 意外添加边距

标签 ios uitableview ios4

我有一个 UITableView,目前它只有一个单元格。我编写了一个自定义的 TableViewCell 类,它继承自 UITableViewCell,以便进行一些自定义绘图。我已经将表格的宽度设置为所需的大小,并且正在尝试将单元格的宽度设置为相同的大小,这样它将填满表格的整个宽度。问题似乎是我在单元格的左侧和右侧获得了一些边距,我不知道为什么。

Here's an example of the problem. 我将 TableView 的背景设为黑色以便更清晰。 TableView 的大小正确。背景图像添加到单元格,而不是表格,它应该占据表格的整个宽度。

我曾尝试使 TableView 更宽(与屏幕一样宽)以尝试适应背景图像的大小,但这并不能完全做到。我宁愿只是弄清楚这些边距从何而来,以及如何摆脱它们。

TableView 本身是在 Interface Builder 中初始化的。样式设置为分组,禁用滚动, View 模式设置为缩放以填充。

这是单元类的 initWithStyle 方法

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier          
{                                                                                                    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {                        
        // Initialization code                                                                       
        _primaryLabel = [[UILabel alloc] init];                                                      
        _primaryLabel.textAlignment = UITextAlignmentLeft;                                           
        _primaryLabel.font = [UIFont systemFontOfSize:18];                                           
        _primaryLabel.backgroundColor = [UIColor clearColor];                                        
        _detailLabel = [[UILabel alloc] init];                                                       
        _detailLabel.textAlignment = UITextAlignmentLeft;                                            
        _detailLabel.font = [UIFont systemFontOfSize:12];                                            
        _detailLabel.backgroundColor = [UIColor clearColor];                                         
        _icon = [[UIImageView alloc] init];                                                          

        [self.contentView addSubview:_primaryLabel];                                                 
        [self.contentView addSubview:_detailLabel];                                                  
        [self.contentView addSubview:_icon];                                                         

        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;                            
        UIImageView* whiteDisclosureView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 15, 13)];
        [whiteDisclosureView setImage:[UIImage imageNamed:@"white_disclosure.png"]];                 
        self.accessoryView = whiteDisclosureView;

        UIImageView * background = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 305, 61)];     
        [background setImage:[UIImage imageNamed:@"button_silver.png"]];
        [self setBackgroundView:background];
        self.backgroundColor = [UIColor clearColor];                                                 

        self.frame = self.contentView.frame = CGRectMake(0, 0, 305, 61);                             
    }   
    return self;                                                                                     
}   

最佳答案

您的 tableView 是否使用“分组”样式?对于分组样式,iOS 通常会为表格单元格添加左右边距。

可以通过将 tableView 的框架调整到稍微超出其父 View 的范围来解决这个问题。 See here for example in previous question

关于iOS UITableView 意外添加边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9388028/

相关文章:

ios - UITableview委托(delegate)方法在json方法之前调用

两个 NSDecimalNumber 对象的 iphone NSComparisonResult 失败

iphone - 如何通过在iphone中编程找到UUID?

iphone - 如何避免隐藏导航栏时出现黑框?

ios - UITabBarController第一次切换tab很慢

objective-c - UItableviewcell 中的按钮

ios - 将数组从 RightChildViewController 传递到 MainVIewController

ios - 是否可以使用 UIPageControl 来控制 UITableView 的移动?

iOS - 多对多数据混淆?

ios - 静默刷新另一个应用程序