我是ios的新手,现在有一天我面临一个小问题。当桌面视图加载后上下滚动然后背景图像显示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;
}
谢谢你提前!
答案 0 :(得分:1)
为此使用自定义委托方法。这是你问题的解决方案......
- (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;
}
答案 1 :(得分:0)
您好,您可以使用resizableImageWithCapInsets,
UIImage *image = [[UIImage imageNamed:@"baloonImage"] resizableImageWithCapInsets:UIEdgeInsetsMake(5.0,10.0,5.0,10.0)];
[self.yourButton setBackgroundImage:image forState:UIControlStateNormal];
修改值(UIEdgeInsetsMake(上,左,下,右))以适合您需要的任何图像。
上面的代码会使图像边角固定,然后在调整图像边框大小以调整视图大小后,
[image setFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
答案 2 :(得分:0)
Haseeb,基于这行代码:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]
我可以看到您没有使用自定义表格视图单元格。
我认为解决此问题的最佳方法是使用自定义表格视图单元格。您可以创建自己的类,您可以在其中设计自己的单元格。
答案 3 :(得分:0)
有几种方法可以做到这一点:1)创建一个自定义UITableViewCell(网上有很多关于如何做到这一点的例子),2)你也可以简单地将你的图像作为一个层添加到单元格中(同样,很多例子。)
例如:
ImageTableViewCell : UITAbleViewCell
@property (strong, nonatomic) UIImage *backgroundImage;
然后,在你的实现中,像这样:
-(void)layoutSubviews
{
[super layoutSubviews];
CALayer *imageLayer = [[CALayer alloc] init];
imageLayer.frame = self.layer.bounds;
// code goes here to do whatever you want to the image ...
imageLayer.contents = self.backgroundImage.CGImage;
[self.layer addSublayer:imageLayer];
}
正如他们所说的那样,思考的食物......
答案 4 :(得分:0)
如上所述,使用自定义原型单元格。然后在Document Outline中选择原型单元格和Attributes检查器。在“将Alpha设置为0”下,将背景颜色设置为“清除颜色”。
答案 5 :(得分:0)
在swift中,您可以将背景图像设置为UITableViewCell,如下所示。
cell.backgroundView = UIImageView(image: UIImage(named: "yourImage.png")!)
将此代码写入表视图的此函数中。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
此图像将重复每一行。