2014-02-11 145 views
0

我创建了一个UITableViewCell,里面有一个UITextView。 我正在使用Json为Textview获取我的Html内容。我也使用DTCoreText。在这里我cellForRowAtIndexPath:单元格内容(Textview)只触摸单元格后更新

static NSString *CellIdentifier1 = @"NewsCell"; 

GTNewsCustomCell *newsCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; 

if(newsCell == nil){ 

    NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"GTNewsCustomCell" 
                 owner:self//transfer ownership to self 
                options:nil]; 
    newsCell = [nibViews objectAtIndex:0]; 
} 

newsCell.cellFrame = CGRectMake(10, 0, 300,40); 

newsCell.titleLabel.text = [[self.newsList objectAtIndex:indexPath.section]objectForKey:@"title"]; 

newsCell.messageTextView.textColor = [UIColor blackColor]; 
newsCell.messageTextView.backgroundColor = GTDefaultTextBackgroundColor; 
newsCell.messageTextView.editable = NO; 
newsCell.messageTextView.userInteractionEnabled = NO; 

newsCell.messageTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 

NSString *htmlTag = @"<b></b>"; 
NSString *html = [NSString stringWithFormat:@"%@%@",htmlTag,[[self.newsList objectAtIndex:indexPath.section]objectForKey:@"previewMessage"]]; 


. 
. 
. 

return newsCell; 

问题的一部分原因是我的TextView的内容有时比TextView的尺寸更大。奇怪的是,当我触摸textview,它更新自己,每个人都显示正确....什么可能是错的?

这里有一些截图:

破碎的TextView: enter image description here

当我接触到的细胞,文本的格式: enter image description here

编辑: 否我已经找到我的问题,但我不知道如何解决它。在我的自定义单元格.m文件我有变革的方法进行的单元尺寸宽度:

- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 
    CGRect cellRect = self.bounds; 
    cellRect.size.width = self.cellFrame.size.width; 

    self.bounds = cellRect; 
} 

如果我删除一切工作正常,但当然我的手机没有正确的大小了......但我需要TableView和Cell之间从左到右的一些空间!

+0

你可以减少一些问题,如在找到最小数量的代码重现问题? –

+1

我希望它更好地知道:) – Davis

回答

0

好吧我终于解决了这个问题:我删除“layoutSubviews”的方法,并插入:

- (void)setFrame:(CGRect)frame { 
    frame.origin.x += 10; 
    frame.size.width -= 2 * 10; 
    [super setFrame:frame]; 
} 

现在我的文字总是有正确的大小和我的手机也有正确的尺寸!

0

我总是认为最好不要试图篡改UITableViewCell的框架/范围。每个单元格的框架由UITableView设置,其宽度等于表格视图的宽度,高度等于表格视图委托人返回的值tableView:heightForRowAtIndexPath:,或者如果未实现,则表格视图的rowHeight属性。

如果您的自定义单元格的内容,即子视图,不是您想要的地方,然后位置和大小这些在您的自定义layoutSubviews实现(或在你的笔尖)。自动布局可以作为一个选项。在你的例子中,你会布置单元的messageTextView

相关问题