2011-07-06 38 views
0

我試圖讓細胞的細節文本居中。detailTextLabel textalignment

我已閱讀所有類型的帖子,但他們似乎都在談論老版本的IOS。我認爲我嘗試了所有帖子的組合,但沒有運氣讓它工作。

[[cell detailTextLabel] setTextAlignment:UITextAlignmentCenter]; 

我想這從willDisplayCell並在下面的代碼,無論是作品。注意兩種方法,我嘗試了這兩種方法。

有誰知道這是否工作,或者我應該創建自己的中心功能(方法)?

static NSString *CellIdentifier = @"Cell"; 


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

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


    cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18.0]; 


    cell.detailTextLabel.font = [UIFont systemFontOfSize:16]; 


    NSMutableDictionary *curRow = [myData objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [curRow objectForKey:@"Description"]; 

    cell.detailTextLabel.text = [curRow objectForKey:@"Stats"]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
     cell.detailTextLabel.textAlignment = UITextAlignmentCenter; 

回答

2

如果對齊方式對您有問題,您可以創建自定義標籤並將子視圖添加到單元格中。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    UILabel *label; 
    UILabel *detailLabel; 

    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
     label = [[[UILabel alloc] initWithFrame:CGRectMake(55, 4, 260, 20)] autorelease]; 
     //make Your alignments to this label 
     label.font = [UIFont boldSystemFontOfSize:15.0]; 
     label.tag=25; 

     //make Your alignments to this detail label 
     detailLabel = [[[UILabel alloc] initWithFrame:CGRectMake(55, 25, 260, 15)] autorelease]; 
     detailLabel.font = [UIFont systemFontOfSize:13.0]; 
     detailLabel.tag=30; 
     [cell.contentView addSubview:label]; 
     [cell.contentView addSubview:detailLabel]; 
    } 
    else 
    { 
     label = (UILabel *)[cell.contentView viewWithTag:25]; 
     detailLabel = (UILabel *)[cell.contentView viewWithTag:30]; 
    } 
    label.text =[curRow objectForKey:@"Description"]; 
    detailLabel.text=[curRow objectForKey:@"Stats"]; 
    return cell; 
} 
+0

非常感謝您的詳細解答! –

+0

我可以做第三個子視圖嗎? –

+0

當你自定義單元格時,你不需要擔心樣式。你想做什麼,就可以做什麼。 – iPrabu

2

另外,如果你想居中detailTextLabel同時還利用自動垂直中心(爲textLabel將垂直居中,如果detailTextLabel是空的),你需要重寫- (void) layoutSubviews

否則標籤的大小將適合內容,因此textAlignment = UITextAlignmentCenter將無法​​正常工作。

- (void) layoutSubviews 
{ 
    [super layoutSubviews]; 
    self.textLabel.frame = CGRectMake(0, self.textLabel.frame.origin.y, self.frame.size.width, self.textLabel.frame.size.height); 
    self.detailTextLabel.frame = CGRectMake(0, self.detailTextLabel.frame.origin.y, self.frame.size.width, self.detailTextLabel.frame.size.height); 
} 
相關問題