簡體   English   中英

如何在ios7的uitableview單元格中設置背景圖像?

[英]how to set background image in uitableview cell in ios7?

我是ios的新手,現在幾天我面臨一個小問題。當加載表視圖時,然后在上下滾動后顯示背景圖像, 如下所示 ,這是我的代碼

實際上,我想將表格顯示為聊天設計。 我不想使用任何第三方。

- (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;
}

謝謝是前進!

為此使用自定義委托方法。 這是您的問題的解決方案...

- (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;
}

嗨,您可以使用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)];

Haseeb,基於以下代碼行:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]

我可以看到您沒有使用自定義表格視圖單元格。

我認為解決此問題的最佳方法是使用自定義表格視圖單元格。 您將創建自己的類,在其中可以設計自己的單元格。

有兩種方法: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];
}

正如他們所說的,值得深思。

如上所述,請使用自定義原型單元。 然后在“文檔大綱”中選擇原型單元格和“屬性”檢查器。 在“視圖”下,將“ Alpha”設置為0,將“背景色”設置為“清除顏色”。

您可以迅速將背景圖片設置為UITableViewCell,如下所示。

cell.backgroundView = UIImageView(image: UIImage(named: "yourImage.png")!)

在表視圖的此功能中編寫此代碼。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 

此圖像將在每一行重復進行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM