创建类似Instagram的照片Feed

时间:2012-01-17 01:16:50

标签: ios uitableview uiwebview feed

我正在尝试为我正在制作的应用制作照片Feed,类似于Instagram:

Instagram photo feed

我使用UITableView为Feed创建了一个初步版本,但是处理每个单元格的动态性质变得麻烦。喜欢和评论的数量以及评论文本本身将决定细胞的高度。我使用UITextViews绘制注释和文本,并使用带有sizeWithFont的UITextViews文本来近似单元格高度和位置。这个解决方案似乎非常不精确,并且有很多缺点。

我正在考虑将整个Feed的UIWebView用作备用解决方案。它可以使定位和评论文本非常简单,并且能够在Instagram中看到文本中的可变字体。我没有真正广泛使用过UIWebViews,所以我不确定以这种方式创建整个feed是多么容易或多难。

我应该继续使用我的UITableView解决方案还是使用UIWebView重新进行重做?

2 个答案:

答案 0 :(得分:1)

在我的应用程序中,我有一个UITableViewCell,它根据单元格中的文本量动态调整其高度。我认为这可能有助于回答你的问题。

以下是诀窍:无需“近似文字大小”---查看[NSString sizeWithFont: constrainedToSize: lineBreakMode:]方法maximumSize = CGSizeMake(self.contentView.bounds.size.width - TEXT_LEFT_MARGIN * 2.0, MAXIMUM_TEXT_HEIGHT); textStringSize = [textLabel.text sizeWithFont:textLabel.font constrainedToSize:maximumSize lineBreakMode:textLabel.lineBreakMode];

CGRect

因此,让我们举一个简单的例子,你想在标签上方有10个像素的空格,在标签下面有15个像素的空白。然后,您将使用上面的度量生成textStringSize.height,其高度为25像素,添加到上面生成的{{1}}值。

使用此方法意味着无论不同标签中的注释大小如何,您的UITableViewCells都可以很好地向上和向下扩展。

答案 1 :(得分:0)

试试这个..

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleIdendifer=@"item";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simpleIdendifer];
    if(cell==nil)
    {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:simpleIdendifer];

    }


    [[cell textLabel] setText:self.str];
    [[cell textLabel] setNumberOfLines:0];
    [[cell textLabel] setLineBreakMode:NSLineBreakByWordWrapping];
    [[cell textLabel] setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleBody]];
        return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //add your table view cell content here
    NSString *string = self.str;

    NSDictionary *attributes = @{NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleBody]};

    CGRect frame = [string boundingRectWithSize:CGSizeMake(CGRectGetWidth(tableView.bounds), CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) attributes:attributes context:nil];
    return ceilf(CGRectGetHeight(frame)+TableViewCellPadding);
}