2012-08-23 48 views
2

我想使它在我的UITableViewCell的文本標籤中的文本從單元格的右側換行約30個像素。然而,它的權利從右到大約5或10像素..我試過編輯所有我已經在cellForRowAtIndex中設置的值,但唯一改變的是單元格的高度,但不是單元格的textLabel有多大是,我無法找到錯誤,希望也許有人能看到錯誤。UITableViewCell textLabel寬度不變

我一直在做進一步的測試,我發現的是,如果我使用sectionIndexTitlesForTableView那麼UItableViewCell textLabel將減少到下面代碼中概述的正確大小..但是,如果我禁用此方法,那麼它不會支付任何費用注意我試圖在下面的代碼中設置寬度。

這是我的代碼如下。

//These Constants are used for dynamic cell height 
#define FONT_SIZE 18.0f 
#define CELL_CONTENT_WIDTH 290.0f 
#define CELL_CONTENT_MARGIN 10.0f 



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    // Configure the cell using custom cell 

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

     [cell.textLabel setLineBreakMode:UILineBreakModeWordWrap]; 
     [cell.textLabel setMinimumFontSize:FONT_SIZE]; 
     [cell.textLabel setNumberOfLines:0]; 
     [cell.textLabel setFont:[UIFont boldSystemFontOfSize:FONT_SIZE]]; 
     [cell.textLabel setTag:1]; 

     [[cell contentView] addSubview:cell.textLabel]; 
    } 

    //Customize cell here 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; // no blue selection 

    //Replaces previously selected cells accessory view (tick) 
    if ([indexPath isEqual:oldCheckedIndexPath]){ 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    }else{ 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    //Display cells with data that has been sorted from startSortingTheArray 
    NSMutableArray *keys = [self.letterDictionary objectForKey:[self.sectionLetterArray objectAtIndex:indexPath.section]]; 

    NSString *key = [keys objectAtIndex:indexPath.row]; 

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f); 

    CGSize size = [key sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 


    //Applise current key value to cell text label 
    [cell.textLabel setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 25.0f))]; 

    [cell.textLabel setText:key]; 

    return cell; 
} 



//Cell size for word wrapping So the user can see all of the details of a submodel etc. 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    //Display cells with data that has been sorted from startSortingTheArray 
    NSArray *keys = [self.letterDictionary objectForKey:[self.sectionLetterArray objectAtIndex:indexPath.section]]; 
    NSString *key = [keys objectAtIndex:indexPath.row]; 

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f); 

    CGSize size = [key sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

    CGFloat height = MAX(size.height, 25.0f); 

    return height + (CELL_CONTENT_MARGIN * 2); 
} 

回答

10

單元格的layoutSubviews方法將覆蓋您在cellForRow(或大多數其他地方)嘗試執行的任何操作。最好的解決方案是繼承UITableViewCell並覆蓋layoutSubviews。確保你叫超級,然後自定義你的文本標籤框架。

編輯:

就包括這個(修改當然)以上的實施和使用

CustomTableViewCell *cell = [tableView dequeReusableCell... 

cell = [[CustomTableViewCell alloc] initWithStyle... 

代替。不需要額外的.xibs。

@interface CustomTableViewCell : UITableViewCell 
    @end 

    @implementation CustomTableViewCell 
    - (void)layoutSubviews 
    { 
     [super layoutSubviews]; 
     cell.textLabel.frame = theFrameYouWant; 
    } 
    @end 
+1

dangit ..我迷路了'layoutSubviews'? – HurkNburkS

+0

其UITableViewCell繼承自UIView的方法。它會在很多情況下自動調用,並且可以手動調用(正確使用setNeedsLayout和/或layoutIfNeeded)。在顯示單元格之前,將調用其layoutSubview方法。這是在視圖自己的框架/邊界發生變化或者首次進入視圖層次結構後配置視圖子視圖框架的正確入口點......您可以查看各種文檔以獲取更多信息。 – Matt

+0

我只是不明白爲什麼它的工作,當我有'sectionIndexTitlesForTableView'啓用,但如果我不啓用它,然後它包裝一個傷心和dosnt的工作。 – HurkNburkS