首页 \ 问答 \ UITableViewCell - 根据最后一个单元格的属性显示文本和背景图像(UITableViewCell - Text and background image are displayed based on last cell's attributes)

UITableViewCell - 根据最后一个单元格的属性显示文本和背景图像(UITableViewCell - Text and background image are displayed based on last cell's attributes)

在我的应用程序中,为iOS7设计,我有一个分组的UITableView。 每行显示用户输入的一些文本。 我根据每行文本的长度计算行高。 每行也有相同的背景图像,但是使用resizableImageWithCapInsets根据行的高度进行拉伸。

以下是我的代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.currentChatView registerClass:[UITableViewCell class] forCellReuseIdentifier:
                                         @"CurrentText"];
    static NSString *CellIdentifier = @"CurrentText";
    UITableViewCell *cell = [self.currentChatView dequeueReusableCellWithIdentifier:
                             CellIdentifier forIndexPath:indexPath];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CellIdentifier];
    }

    UIImage *cellImage;
    UIImageView *cellImageView = [[UIImageView alloc]init];
    UILabel *cellLabel = [[UILabel alloc]init];

    cellLabel.numberOfLines = 0;
    cellLabel.lineBreakMode = NSLineBreakByWordWrapping;
    cellLabel.text = [[self.textArray 
                       objectAtIndex:indexPath.row] objectForKey:@"text"];

    UIImage *image = [UIImage imageNamed:@"aqua.png"];
    UIEdgeInsets insets = UIEdgeInsetsMake(image.size.height/2, image.size.width/2, 
                                           image.size.height/2, image.size.width/2);
    cellImage = [image resizableImageWithCapInsets:insets];
    cellImageView.frame = CGRectMake(0, 0, 215, cellHeight + 5);
    cellLabel.frame = CGRectMake(10, 0, 200, cellHeight);

    cellImageView.image = cellImage;
    [cell.contentView addSubview:cellImageView];
    [cell.contentView addSubview:cellLabel];

    return cell;
}

表行高度计算并正确显示。 但是,文本和背景基于为最后一行计算的高度显示。

例如,如果第二行有两行文本而最后一行只有一行,则第二行显示高度为2行,但只有文本和图像的上半部分显示在行中。 该行的下半部分为空,背景为白色。 最后一行显示正确。

同样,如果最后一行有两行文本,则所有行的图像背景都会被拉伸以填充两行文本。

我不确定原因是什么,无法在任何地方找到解决方案。 非常欢迎您的建议。


In my app, designing for iOS7, I have a grouped UITableView. Each row displays some text entered by the user. I calculate the row height based on the length of the text in each row. Each row also got same background image, but stretched using resizableImageWithCapInsets based on the height of the row.

Below is my code.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.currentChatView registerClass:[UITableViewCell class] forCellReuseIdentifier:
                                         @"CurrentText"];
    static NSString *CellIdentifier = @"CurrentText";
    UITableViewCell *cell = [self.currentChatView dequeueReusableCellWithIdentifier:
                             CellIdentifier forIndexPath:indexPath];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CellIdentifier];
    }

    UIImage *cellImage;
    UIImageView *cellImageView = [[UIImageView alloc]init];
    UILabel *cellLabel = [[UILabel alloc]init];

    cellLabel.numberOfLines = 0;
    cellLabel.lineBreakMode = NSLineBreakByWordWrapping;
    cellLabel.text = [[self.textArray 
                       objectAtIndex:indexPath.row] objectForKey:@"text"];

    UIImage *image = [UIImage imageNamed:@"aqua.png"];
    UIEdgeInsets insets = UIEdgeInsetsMake(image.size.height/2, image.size.width/2, 
                                           image.size.height/2, image.size.width/2);
    cellImage = [image resizableImageWithCapInsets:insets];
    cellImageView.frame = CGRectMake(0, 0, 215, cellHeight + 5);
    cellLabel.frame = CGRectMake(10, 0, 200, cellHeight);

    cellImageView.image = cellImage;
    [cell.contentView addSubview:cellImageView];
    [cell.contentView addSubview:cellLabel];

    return cell;
}

Table row heights are calculated and displayed correctly. However, the text and background are shown based on the height calculated for the last row.

For example, if the 2nd row got two lines of text and the last row got only one line, then the second row is displayed with a height for 2 lines, but only the upper half of text and image are displayed in the row. The lower half of the row is empty with white background. The last row is displayed correctly.

Similarly, if the last row has two lines of text, then the image background of all rows are stretched to fill two lines of text.

I am not sure what the cause is and couldn't find a solution anywhere. Your suggestions are very welcome.


原文:https://stackoverflow.com/questions/20587185
更新时间:2022-03-09 21:03

最满意答案

我只删除了一些样式才能理解代码。 这个解决方案适合你吗?

.wrapper {
  position:relative;
  width:728px;
  max-width:100%;
}
.wrapper:after {
  content: ' ';
  clear: both;
  display: block;
}

#main_article_box {
  float:left;
  width:100%;
  height:auto;
  border:0 none;
  padding-top:51.37%;
  background-size:cover;
}


#main_article_text {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  margin: 0 3px 3px 3px;
  height: 90px;
  background: #ffffff;
  border: 1px solid #f8f8f8;
  background: #ffffff;
  background: -moz-linear-gradient(#ffffff, #cccccc); 
  background: -webkit-gradient(linear,left bottom,left     top,color-stop(0, #cccccc),color-stop(1, #ffffff));	
  background: -webkit-linear-gradient(#ffffff, #cccccc);	
  background: -o-linear-gradient(#ffffff, #cccccc);
  background: -ms-linear-gradient(#ffffff, #cccccc);
  background: linear-gradient(#ffffff, #cccccc);
  /*-moz-border-radius: 1px;
  border-radius: 1px;
  -moz-box-shadow: 0 1px 1px #f8f8f8;
  -webkit-box-shadow: 0 1px 1px #f8f8f8;
  box-shadow: 0 1px 1px #f8f8f8;*/
  opacity:0.8;
  filter:alpha(opacity=80);
}

#main_article_title {
  font-size: 23px;
  letter-spacing: -1px;
  text-transform:uppercase;
  text-shadow: #333;
  font-weight: bold;
  margin: 8px;
}

#main_article_short_text {
  font-size: 14px;
  width: auto;
  font-weight:bold;
  margin: -2px 0 0 8px;
  color: #333333;
}

@media only screen and (max-width: 460px) {


  #main_article_box {

    width: 100%;
    height: auto;

  }


  #main_article_text {
    width: 98%;
    height: auto;


  }

  #main_article_title {
    font-size: 16px;


  }

  #main_article_short_text {
    font-size: 11px;

  }  

}
<div class="wrapper">
  <div id="main_article_box" style="background-image: url(http://www.watchathletics.com/data/headline/9718/rio%20usain%20bolt.jpg);">
    <div id="main_article_text">
      <div id="main_article_title"><a href="http://www.w3schools.com/html/">Main Article Title for this post</a></div>
      <div id="main_article_short_text"><p>This is some intro text the article below title. This is some intro text the article below title. </p>...</div>
    </div>
  </div>
</div>


I removed some styles only for me to understand the code. Is this solution correct for you?

.wrapper {
  position:relative;
  width:728px;
  max-width:100%;
}
.wrapper:after {
  content: ' ';
  clear: both;
  display: block;
}

#main_article_box {
  float:left;
  width:100%;
  height:auto;
  border:0 none;
  padding-top:51.37%;
  background-size:cover;
}


#main_article_text {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  margin: 0 3px 3px 3px;
  height: 90px;
  background: #ffffff;
  border: 1px solid #f8f8f8;
  background: #ffffff;
  background: -moz-linear-gradient(#ffffff, #cccccc); 
  background: -webkit-gradient(linear,left bottom,left     top,color-stop(0, #cccccc),color-stop(1, #ffffff));	
  background: -webkit-linear-gradient(#ffffff, #cccccc);	
  background: -o-linear-gradient(#ffffff, #cccccc);
  background: -ms-linear-gradient(#ffffff, #cccccc);
  background: linear-gradient(#ffffff, #cccccc);
  /*-moz-border-radius: 1px;
  border-radius: 1px;
  -moz-box-shadow: 0 1px 1px #f8f8f8;
  -webkit-box-shadow: 0 1px 1px #f8f8f8;
  box-shadow: 0 1px 1px #f8f8f8;*/
  opacity:0.8;
  filter:alpha(opacity=80);
}

#main_article_title {
  font-size: 23px;
  letter-spacing: -1px;
  text-transform:uppercase;
  text-shadow: #333;
  font-weight: bold;
  margin: 8px;
}

#main_article_short_text {
  font-size: 14px;
  width: auto;
  font-weight:bold;
  margin: -2px 0 0 8px;
  color: #333333;
}

@media only screen and (max-width: 460px) {


  #main_article_box {

    width: 100%;
    height: auto;

  }


  #main_article_text {
    width: 98%;
    height: auto;


  }

  #main_article_title {
    font-size: 16px;


  }

  #main_article_short_text {
    font-size: 11px;

  }  

}
<div class="wrapper">
  <div id="main_article_box" style="background-image: url(http://www.watchathletics.com/data/headline/9718/rio%20usain%20bolt.jpg);">
    <div id="main_article_text">
      <div id="main_article_title"><a href="http://www.w3schools.com/html/">Main Article Title for this post</a></div>
      <div id="main_article_short_text"><p>This is some intro text the article below title. This is some intro text the article below title. </p>...</div>
    </div>
  </div>
</div>

相关问答

更多
  • 不,它不可能使div适应它的背景图像。 因为这是'无意义的' 这是如何: div的大小由其“内容”决定,或者其尺寸是特定设置的。 由于背景图像不属于任何这些类别,因此您可以看到这是不可能的。 你可以做的是这样的: HTML
    ...
    ...
    CSS .image-controlled { position: relative; ove ...
  • 高度百分比不会工作,除非任何父元素的像素高度。 我的建议是使用img标签而不是background-image使其响应。 但是,如果您只能将div与背景图片一起使用,那么您可以使用javascript更改窗口大小调整事件中div的高度。 就像图像是4:3的比例,代码将如下所示: