0

我想要实现的是这样的。它是一种 Facebook 或 twitter 功能,其中您有一个表格视图,其中您有一个线程,并且线程包含不同数量的文章。每行的文章数量各不相同。所以基本上它就像我在 facebook 上发送一个帖子,人们回复那个帖子,这些帖子被添加到那个特定的线程下(我只是关心如何显示它,其他所有事情都得到了照顾)。这里是图片在此处输入图像描述

我知道如何创建单元格,但我不知道如何动态设置其大小。任何教程或任何建议来实现这一目标?

任何帮助表示赞赏..

谢谢

4

3 回答 3

1

您可以将其用于动态单元格高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath;

您可以使用 switch 案例并检查indexPath.rowindexPath.section以返回所需的高度

于 2012-09-13T06:09:41.343 回答
1

在 .h 文件中获取自定义 UITableViewCell

 @interface PartnerCell : UITableViewCell {
        UILabel  *user_name,*lbldate,*lbldesc;
        LoadImage *img_trade;
        UIImageView *partnerimage;
    }
    @property (nonatomic, strong) UILabel *user_name,*lbldate,*lbldesc;
    @property (nonatomic, strong) LoadImage *img_trade;
    @property (nonatomic, strong) UIImageView *partnerimage;
    @end

在 .m 文件中,

#import "PartnerCell.h"

@implementation PartnerCell
@synthesize user_name,lbldate,lbldesc;
@synthesize img_trade,partnerimage;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {   

        [self setSelectionStyle:UITableViewCellSelectionStyleNone];

        user_name = [[UILabel alloc] initWithFrame:CGRectMake(75,8,200,15)];
        [user_name setBackgroundColor:[UIColor clearColor]];
        user_name.font = [UIFont fontWithName:@"Arial-BoldMT" size:15];
        [user_name setTextColor:[UIColor colorWithRed:70/255.00f green:70/255.00f blue:70/255.00f alpha:1.0]];
        [self addSubview:user_name];

        lbldate = [[UILabel alloc] initWithFrame:CGRectMake(75,28,200,15)];
        [lbldate setBackgroundColor:[UIColor clearColor]];
        lbldate.font = [UIFont fontWithName:@"Arial" size:14];
        [lbldate setTextColor:[UIColor darkGrayColor]];
        [self addSubview:lbldate];


        lbldesc = [[UILabel alloc] initWithFrame:CGRectMake(75,45,170,35)];
        [lbldesc setBackgroundColor:[UIColor clearColor]];
        lbldesc.font = [UIFont fontWithName:@"Arial" size:13];
        lbldesc.numberOfLines = 2;
        [lbldesc setTextColor:[UIColor darkGrayColor]];
        [self addSubview:lbldesc];


        img_trade = [[LoadImage alloc] initWithFrame:CGRectMake(3, 5, 54, 55)];
        img_trade.userInteractionEnabled = YES;
        [self addSubview:img_trade];



    }
    return self;
}

在主表视图类中编写此代码,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

NSString *CellIdentifier = [NSString stringWithFormat:@"%@",[[Partarray objectAtIndex:indexPath.row-1] valueForKey:@"part_id"]];
cell = (PartnerCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell=nil;
if (cell == nil)
{   
    cell = [[PartnerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[Partarray objectAtIndex:indexPath.row]] autorelease];
    cell.selectionStyle= UITableViewCellSelectionStyleGray;
    cell.backgroundColor = [UIColor clearColor];
    cell.user_name.frame=CGRectMake(75,8,340,15);
    cell.lbldate.frame=CGRectMake(75,28,340,15);
    cell.lbldesc.frame=CGRectMake(75,45,340,35);


    cell.user_name.text = @"user name";
    cell.lbldate.text = @"date";
    cell.lbldesc.text = @"description";



}
    return cell;
}

获取条件并根据该条件添加对象数量。

于 2012-09-13T07:27:04.057 回答
1

也许这会对你有所帮助。我正在向您发送我的代码,我正在使用一个自定义单元格,它在第一个索引上有一个 Emial,在第二个索引上有一个 PhoneNumber,在第三个索引上有一个地址(在你的情况下是文章)。我正在动态更改 CellForRowAtIndexPath 中地址标签的高度,以及 heightForRowAtIndexPath 方法中的单元格高度。这是我的代码。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *identifier = @"FeedCell";
    FeedDetailCell *cell = (FeedDetailCell*)[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
        cell = (FeedDetailCell*)[[[NSBundle mainBundle] loadNibNamed:@"FeedDetail" owner:nil options:nil] objectAtIndex:0];
    }
    [tableView setSeparatorColor:[UIColor grayColor]];
    switch (indexPath.section) {
        case 0:
            if (indexPath.row == 0) {
                cell.nameLabel.text = @"Telephone";
                [cell.detailLabel setText:[_feedDictionary valueForKey:@"mobile"]];
            }
            else {
                cell.nameLabel.text = @"Mobile";
                [cell.detailLabel setText:[_feedDictionary valueForKey:@"iPhone"]];
            }
            break;
        case 1:
            cell.nameLabel.text = @"E-mail";
            [cell.detailLabel setText:[_feedDictionary valueForKey:@"Email"]];
            break;
        case 2:
            cell.nameLabel.text = @"address";
            [cell.detailLabel setText:[_feedDictionary valueForKey:@"address"]];
            CGSize size = [[_feedDictionary valueForKey:@"address"] sizeWithFont:[UIFont systemFontOfSize:14.0]
                               constrainedToSize:CGSizeMake(200.0, 400.0) lineBreakMode:UILineBreakModeWordWrap];
            CGRect frm = cell.detailLabel.frame;
            frm.size.height = size.height;
            [cell.detailLabel setFrame:frm];
        default:
           break;
    }
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 2) {
        NSString *address = [_feedDictionary valueForKey:@"address"];
        CGSize recommendedSize = [address sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(320, INT_MAX)];
        return 44 + recommendedSize.height;
    }
    else {
        return 44;
    }
}
于 2012-09-13T07:36:35.480 回答