作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 ios 的新手,现在有几天我遇到了一个小问题。当加载 TableView 然后上下滚动后背景图像显示 like this这是我的代码
实际上我想将表格显示为聊天设计。我不想使用任何第三方。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_messages count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *comment = [_messages objectAtIndex:indexPath.row];
CGFloat whidt = 250;
UIFont *FONT = [UIFont systemFontOfSize:18];
NSAttributedString *attributedText =[[NSAttributedString alloc] initWithString:comment attributes:@ { NSFontAttributeName: FONT }];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){whidt, MAXFLOAT}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize size = rect.size;
return size.height +50;
}
- (UIFont *)fontForCell{
return [UIFont boldSystemFontOfSize:18.0];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"ChatListItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *text = [_messages objectAtIndex:indexPath.row];
CGSize constraint = CGSizeMake(250 , 20000.0f);
UIFont *FONT = [UIFont systemFontOfSize:11];
CGSize size = [text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:FONT }context:nil].size;
cell.textLabel.frame = CGRectMake(10, 0, 250, MAX(size.height, 54.0f) + 20.0f);
cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.text = text;
cell.textLabel.font = [UIFont systemFontOfSize:18];
UIImage *img = [UIImage imageNamed:@"balloon_selected_right.png"];
CGSize imgSize = cell.textLabel.frame.size;
UIGraphicsBeginImageContext( imgSize );
[img drawInRect:CGRectMake(0,0,250,imgSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.textLabel.backgroundColor = [UIColor colorWithPatternImage:newImage];
/*[cell.textLabel setText:[_messages objectAtIndex:indexPath.row]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = [_messages objectAtIndex:indexPath.row];*/
return cell;
}
提前致谢!
最佳答案
为此使用自定义委托(delegate)方法。这是您的问题的解决方案...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Configure the cell in each row
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [self getCellContentView:CellIdentifier];
UILabel *textLabel = (UILabel *)[cell viewWithTag:1];
[textLabel setText:@""];
textLabel.text = @"some text message to show"; // your text string
return cell;
}
- (UITableViewCell *)getCellContentView:(NSString *)cellIdentifier
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
cell.backgroundColor=[UIColor clearColor];
CGRect labelRect = CGRectMake(20, 5, 100, 30); // set your desired values here
UIImage *img = [UIImage imageNamed:@"image.png"];
CGSize imgSize = labelRect.size;
UIGraphicsBeginImageContext( imgSize );
[img drawInRect:CGRectMake(0,0,250,imgSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UILabel *textLabel = [[UILabel alloc] initWithFrame:labelRect];
textLabel.tag=1;
textLabel.font=[UIFont fontWithName:@"Superclarendon" size:15];
// some of your other constraints to set ...
textLabel.backgroundColor=[UIColor colorWithPatternImage:newImage];
[cell.contentView addSubview:textLabel];
return cell;
}
关于ios - 如何在 ios7 的 uitableview 单元格中设置背景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26315167/
我是一名优秀的程序员,十分优秀!