Ios 单元格内容(Textview)仅在触摸单元格后更新
我创建了一个UITableViewCell,里面有一个UITextView。 我正在使用Json获取Textview的Html内容。我也在使用DTCoreText。这里是我的Ios 单元格内容(Textview)仅在触摸单元格后更新,ios,iphone,uitableview,uitextview,Ios,Iphone,Uitableview,Uitextview,我创建了一个UITableViewCell,里面有一个UITextView。 我正在使用Json获取Textview的Html内容。我也在使用DTCoreText。这里是我的cellforrowatinexpath: static NSString *CellIdentifier1 = @"NewsCell"; GTNewsCustomCell *newsCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
cellforrowatinexpath:
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;
如果我删除这个,一切正常,但当然我的单元格不再有正确的大小…但我需要在TableView和单元格之间从左到右留出一些空间 确定最后我解决了这个问题:我删除了“layoutSubviews”方法,并插入:
- (void)setFrame:(CGRect)frame {
frame.origin.x += 10;
frame.size.width -= 2 * 10;
[super setFrame:frame];
}
现在我的文本总是有正确的大小,我的单元格也有正确的大小 我一直认为最好不要试图篡改
UITableViewCell
的帧/边界。每个单元格的框架由UITableView
设置,宽度等于表视图的宽度,高度等于表视图委托的tableView:heightforrowatinexpath:
返回的值,或者如果未实现,则等于表视图的rowHeight
属性
如果自定义单元格的内容(即子视图)不在所需位置,则在自定义
layoutSubviews
实现(或nib)中定位并调整其大小。这里可以选择自动布局。在您的示例中,您可以布置单元格的messageTextView
是否可以减少问题一些,例如,找到最小数量的代码来重现问题?我希望您能更好地了解:)
- (void)setFrame:(CGRect)frame {
frame.origin.x += 10;
frame.size.width -= 2 * 10;
[super setFrame:frame];
}