2014-07-04 57 views
1

我開發了iPhone應用程序,其中我有UITableView。UITableView textlabel框架顯示不正確

的UITableViewCell textlabel幀根據文本是不妥當的,

當我寫此代碼,UITableView爲textLabel是不妥當的。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

     static NSString *CategCellIdentifier = @"Cell"; 
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CategCellIdentifier]; 
        if (cell == nil) { 
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CategCellIdentifier]; 
        } 

       cell.textLabel.backgroundColor=[UIColor redColor]; 
       cell.textLabel.font= [UIFont systemFontOfSize:12.0]; 
       cell.textLabel.textAlignment = NSTextAlignmentCenter; 
       cell.textLabel.text = [tempArr objectAtIndex:indexPath.row]; 

       return cell; 
     } 

當我通過我的數組的UITableView它顯示是這樣的:

請告訴我這個問題?我在哪裏做的錯誤,請幫

回答

1

我不知道什麼是文本的數組包含的內容。但是,我創造了一個我自己的例子。下面是代碼:

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 10; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CategCellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CategCellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CategCellIdentifier]; 
    } 

    cell.textLabel.backgroundColor = [UIColor redColor]; 
    cell.textLabel.font= [UIFont systemFontOfSize:20.0]; 
    cell.textLabel.textAlignment = NSTextAlignmentCenter; 
    cell.textLabel.text = @"Brand"; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    cell.textLabel.backgroundColor = [UIColor redColor]; 
} 

@end 

這裏是輸出:

enter image description here

我用你的代碼。唯一不同的是,我將紅色應用於UITableViewDelegate的willDisplayCell:方法中的文本標籤。

即將出現在桌面視圖兩邊的邊緣按照Apple的UI準則。

讓我知道你是否需要我的代碼。

+0

我的問題不是'cell.backgroundColor'問題是爲什麼文本標籤獲得不正確的高度,'cell.textLabel.backgroundColor = [UIColor redColor];'只顯示理解。 – Krunal

+0

@Krunal我編輯了我的答案。請檢查。 –

+0

看到你的形象,你也面臨着與我的相同的問題,看到你的圖像中的極端左和極右的空白正在發生...... – Krunal

0

嘗試設置大小以適應內容設置文本之後。

[cell.textLabel sizeToFit]; 

謝謝。

+0

老兄還存在同樣的問題 – Krunal

+0

我在面對textlabel檢查寬度的問題在這裏,http://i.imgur.com/GvflcZX.png – Krunal

0

檢查表中尺寸檢查行高屬性,可能有些東西錯了,

[的tableView setRowHeight:100.00]。

,或者您可以使用委託來設置行的高度

  • (CGFloat的)的tableView:(UITableView的*)的tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
+0

我面臨的問題在這裏的寬度textlabel檢查http:///i.imgur.com/GvflcZX.png – Krunal