2013-10-09 66 views
-1

我的方法:無法設置的UITableView細胞標籤背景顏色和文本對齊

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    NSInteger row = indexPath.row; 

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

    // Setup row background image (a.k.a the set image) 
    NSString *setID = [[self.wallpaperCollectionArray objectAtIndex:row] valueForKey:@"id"]; 
    NSString *setImageName = [@"s-" stringByAppendingString:setID]; 
    UIImage *setImage = [UIImage imageNamed:setImageName]; 

    cell.backgroundView = [[UIImageView alloc] initWithImage:setImage]; 
    cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:setImage]; 

    NSString *setName = [[self.wallpaperCollectionArray objectAtIndex:row] valueForKey:@"name"]; 
    cell.textLabel.text = [setName uppercaseString]; 
    cell.textLabel.font = [UIFont fontWithName:@"OpenSans" size:20]; 
    cell.textLabel.textColor = [UIColor whiteColor]; 
    cell.textLabel.backgroundColor = [UIColor redColor]; // Not working? 
    cell.textLabel.textAlignment = NSTextAlignmentCenter; // Not working? 

    return cell; 
} 

然而,結果是相當怪異,其中backgroundColortextAlignment沒有得到應用。

meh!

這是一個編程方式創建的UITableView。我該怎麼辦?

UPDATE:背景顏色問題已解決。仍然難以對中。

+0

你的部署目標是什麼? –

+0

我相信[這篇文章是相同的問題](http://stackoverflow.com/questions/1164459/changing-uitableviewcell-textlabel-background-color-to-clear),並會幫助你。 – Mick

+0

http://stackoverflow.com/questions/2610547/textlabel-backgroundcolor-on-uitableviewcell-does-not-work –

回答

0

通過改變UITableViewCellStyleValue1發現定心問題解決UITableViewCellStyleDefault

if (nil == cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier]; 
    } 
0
if (nil == cell) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellIdentifier]; 
} 

NSTextAlignment不適用於樣式UITableViewCellStyleValue1和UITableViewCellStyleDefault。

因此,如果您需要對齊單元格中的textLabel,請使用UITableViewCellStyleValue2。

希望它有幫助!

+0

這似乎不起作用(並且這樣做也切斷了表單元格上的文本:「BOLD ...」,「DREAM ...」等等)。 – hfz

0

建議:不要以編程方式進行此對齊,我會建議您根據需要創建一個包含背景圖像和所有其他控件的包含nib文件的自定義單元。如果您需要更改UI,這將在未來幫助您。在xib文件中,你也可以直接設置文本對齊。

0

嘗試這種方式..

首先設置框架細胞

- (void)layoutSubviews 
{ 
    CGRect textLabelFrame = self.textLabel.frame; 
    [super layoutSubviews] 

    textLabelFrame.size.width=250.0f; 
    self.textLabel.frame = textLabelFrame; 
} 

那麼我認爲這將中心對準工作。

讓我知道你是否有任何問題。

+0

這對您有幫助嗎? – user1673099

+0

我在哪裏放這個方法? – hfz

+0

@hfz你把它放在一個UITableViewCell子類中,而不是從你發佈的tableView:cellForRowAtIndexPath:方法中返回該子類。 –

0

我建議添加您自己的UILabel作爲一個子視圖,並只使用它,從來沒有cell.textLabel。這樣你就可以完全控制(沒有任何干擾)文本對齊和視圖框架。

實施例:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    NSInteger row = indexPath.row; 

    if (nil == cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier]; 

     /* 
     * Make the label cover the entire bounds of the cell, no matter how the cell resizes 
     * (Old school autoresizing still works because it gets translated into constraints) 
     */ 
     UILabel *label = [[UILabel alloc] initWithFrame:cell.contentView.bounds]; 
     label.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 

     /* 
     * No need to set this stuff repeatedly, let's just do it 
     * once on creation of the label 
     */ 
     label.font = [UIFont fontWithName:@"OpenSans" size:20]; 
     label.textColor = [UIColor whiteColor]; 
     label.backgroundColor = [UIColor redColor]; // Should work now 
     label.textAlignment = NSTextAlignmentCenter; // Should work now 

     // Set a tag so we can find this label later 
     label.tag = 42; 
    } 

    // Setup row background image (a.k.a the set image) 
    NSString *setID = [[self.wallpaperCollectionArray objectAtIndex:row] valueForKey:@"id"]; 
    NSString *setImageName = [@"s-" stringByAppendingString:setID]; 
    UIImage *setImage = [UIImage imageNamed:setImageName]; 

    cell.backgroundView = [[UIImageView alloc] initWithImage:setImage]; 
    cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:setImage]; 

    NSString *setName = [[self.wallpaperCollectionArray objectAtIndex:row] valueForKey:@"name"]; 

    // Find our custom UILabel and configure it 
    UILabel *label = (id)[self.contentView viewWithTag:42]; 
    label.text = [setName uppercaseString]; 

    return cell; 
} 

更好的是,使用一個UITableViewCell子類來代替。它使你的代碼更有條理。在這種情況下,您可以使用@ user1673099的建議。