如何在分组表视图的行中添加更多2个或更多标签

时间:2011-05-21 12:42:07

标签: iphone objective-c xcode4 uitableview

如何在分组表视图的一行中添加2个以上的标签?

我使用了以下代码,但它只显示最后一个标签

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";

    NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];


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

    }

    NSUInteger row = [indexPath row];
    cell.textLabel.text = [listData objectAtIndex:row];


    switch(indexPath.section)
    {

        case 0:
            switch (indexPath.row) 
        {
            case 0:
            {

                lbl = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,50, 10)] autorelease];
                cell.accessoryView = lbl;
                cell.textLabel.text=@"Company:"; 
                lbl.tag = indexPath.row;


                UILabel* lbl1 = [[[UILabel alloc] initWithFrame:CGRectMake(0,12,50, 10)] autorelease];
                cell.accessoryView = lbl1;
                cell.textLabel.text=@"Account:";
                lbl1.tag = indexPath.row;


                UILabel* lbl2 = [[[UILabel alloc] initWithFrame:CGRectMake(0,24,50, 10)] autorelease];
                cell.accessoryView = lbl2;
                cell.textLabel.text=@"Other ID:";
                lbl2.tag = indexPath.row;
            } 
                break;
            case 1: 
            {  /*
                lbl = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,50, 10)] autorelease];
                cell.accessoryView = lbl;
                cell.textLabel.text=@"Add New Contact:";  
                */
            } 
                break;

}
}

要增加行高,我使用以下代码:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch(indexPath.section)
    {
        case 0:
        {
            switch(indexPath.row)
            {
                case 0:
                {
                    return 100;
                }
                    break;

                case 1:
                {
                    return 40;
                }
                    break;

                case 2:
                {
                    return 40;
                }
                    break;

                case 3:
                {
                    return 40;
                }
                    break;

                default:
                {
                    return 15;
                }

            }
        }
            break;

}
}}

1 个答案:

答案 0 :(得分:1)

你可以使用自定义单元格,并且可以在你的单元格中有任何东西,不仅有两个标签,还有任何东西.....这里就你的问题而言......

我们要做的第一件事是创建一个customCell。右键单击Classes并添加一个新的UITableViewCell子类。命名为“CustomCell”。现在打开CustomCell.h并添加以下代码:

@interface CustomCell : UITableViewCell {

UILabel *primaryLabel;

UILabel *secondaryLabel;

UIImageView *myImageView;

}

@property(nonatomic,retain)UILabel *primaryLabel;

@property(nonatomic,retain)UILabel *secondaryLabel;

@property(nonatomic,retain)UIImageView *myImageView;

@end

合成CustomCell.m中的所有三个元素,因为我们将从其他类中访问这些元素。

@synthesize primaryLabel,secondaryLabel,myImageView;

打开CustomCell.m并添加以下代码

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {

if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {

// Initialization code

primaryLabel = [[UILabel alloc]init];

primaryLabel.textAlignment = UITextAlignmentLeft;

primaryLabel.font = [UIFont systemFontOfSize:14];

secondaryLabel = [[UILabel alloc]init];

secondaryLabel.textAlignment = UITextAlignmentLeft;

secondaryLabel.font = [UIFont systemFontOfSize:8];

myImageView = [[UIImageView alloc]init];

[self.contentView addSubview:primaryLabel];

[self.contentView addSubview:secondaryLabel];

[self.contentView addSubview:myImageView];

}

return self;

}


set the frames in layoutsubviews

    - (void)layoutSubviews {

[super layoutSubviews];

CGRect contentRect = self.contentView.bounds;

CGFloat boundsX = contentRect.origin.x;

CGRect frame;

frame= CGRectMake(boundsX+10 ,0, 50, 50);

myImageView.frame = frame;

frame= CGRectMake(boundsX+70 ,5, 200, 25);

primaryLabel.frame = frame;

frame= CGRectMake(boundsX+70 ,30, 100, 15);

secondaryLabel.frame = frame;

}

现在转到tableViewController并使用您的标签

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

static NSString *CellIdentifier = @”Cell”;

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

}

// Set up the cell…

switch (indexPath.row) {

case 0:

cell.primaryLabel.text = @"Meeting on iPhone Development";

cell.secondaryLabel.text = @"Sat 10:30";

cell.myImageView.image = [UIImage imageNamed:@"meeting_color.png"];

break;

case 1:

cell.primaryLabel.text = @"Call With Client";

cell.secondaryLabel.text = @"Planned";

cell.myImageView.image = [UIImage imageNamed:@"call_color.png"];

break;

case 2:

cell.primaryLabel.text = @"Appointment with Joey";

cell.secondaryLabel.text = @"2 Hours";

cell.myImageView.image = [UIImage imageNamed:@"calendar_color.png"];

break;

case 3:

cell.primaryLabel.text = @"Call With Client";

cell.secondaryLabel.text = @"Planned";

cell.myImageView.image = [UIImage imageNamed:@"call_color.png"];

break;

case 4:

cell.primaryLabel.text = @"Appointment with Joey";

cell.secondaryLabel.text = @"2 Hours";

cell.myImageView.image = [UIImage imageNamed:@"calendar_color.png"];

break;

default:

break;

}

return cell;

}

当然,更改图像名称//,.......问候