如何在ios中为Tableview创建动态表格单元格

时间:2012-09-13 06:00:34

标签: objective-c ios uitableview

我想要实现的是这样的。它有一种Facebook或Twitter功能,你有一个tableview,你有一个线程,该线程包含不同数量的文章。每行的文章数量各不相同。所以基本上就像我在Facebook上发帖子一样,人们回复那个帖子,那些帖子被添加到那个特定的帖子下面(我只关心如何显示它,其他所有东西都被照顾)。图片enter image description here

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

感谢任何帮助..

感谢

3 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:1)

采取自定义UITableViewCell, 在.h文件中

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

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

答案 2 :(得分:1)

也许这会对你有所帮助。我发给你的是我的代码,我正在使用自定义单元格,它在第一个索引上有一个Emial,在第二个索引上有PhoneNumber,在第三个索引上有一个Address(在你的案例文章中)。我动态地改变CellForRowAtIndexPath中地址标签的高度,以及heightForRowAtIndexPath mehthod中的单元格高度。这是我的代码。

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