2014-05-12 102 views
0

我为我的通用应用程序制作了自定义单元格。在iPhone屏幕文本是正常的,但当我移动到iPad屏幕文本从最左边开始(如下图所示)。我怎么能把它移动一点(也许10px)。有没有办法从012px 方法中的10px开始文本标签?提前致谢。UITableViewCell中的文本对齐

iPad UITableView Screen

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell2"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    cell.textLabel.text = [firstArray objectAtIndex:indexPath.row]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    cell.textLabel.numberOfLines = 0; 
    [cell.textLabel setLineBreakMode:NSLineBreakByWordWrapping]; 


    UIImage *background = [self cellBackgroundForRowAtIndexPath:indexPath]; 

    cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, cell.bounds.size.width); 

    UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background]; 
    cellBackgroundView.image = background; 
    cell.backgroundView = cellBackgroundView; 

    return cell; 
} 
+0

你怎么现在设置你的细胞?发布一些如何创建自定义单元格的代码 – Stonz2

回答

0

如果你想覆盖该领域的文本插入,覆盖“-editingRectForBounds:”

- (CGRect)editingRectForBounds:(CGRect)bounds { 
    return CGRectInset(bounds , 10 , 10); 
} 

如果要移动的标签的单元格内,你可以创建自定义的UITableViewCell类并覆盖布局子视图

- (void) layoutSubviews { 
    [super layoutSubviews]; 
    self.textLabel.frame = CGRectMake(10, 0, 310, 20); 
} 

该单元格可以初始化这样

cell = [[YOUR_CUSTOM_CELL_CLASS alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

编辑

在你的表视图控制器:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //Dequeue if possible of course 
    UITableViewCell *cell = [[CustomCellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Identifier"]; 

    cell.textLabel.text = @"Should be indented"; 

    return cell; 
} 

在你CustomCellClass:

- (void) layoutSubviews { [super layoutSubviews]; self.textLabel.frame = CGRectMake(100, 0, 310, 20); }

然后文本标签将缩进100分:)

来源: 只是没有

+0

我创建了一个自定义单元类并覆盖了layoutSubviews,但它不起作用。 –