开发者

how to add more 2 or more labels in a row of grouped table view

开发者 https://www.devze.com 2023-03-07 23:21 出处:网络
How can I add more than 2 labels into a row of a grouped table view? I used the following code but it\'s displaying only thelast label

How can I add more than 2 labels into a row of a grouped table view?

I used the following code but it's displaying only the last label

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

}
}

To increase the row height, I am using following code:

- (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:开发者_Go百科
                {
                    return 15;
                }

            }
        }
            break;

}
}}


you can use custom cell and can have anything in your cell, not only two labels, but any thing.....here it goes as far as your problem is concerned......

First thing we will do is create a customCell. Right click on Classes and add a new UITableViewCell subclass. Name as “CustomCell”. Now open CustomCell.h and add the following code:

@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

synthesize all the three elements in CustomCell.m as we are going to access these elements from other classes.

@synthesize primaryLabel,secondaryLabel,myImageView;

Open CustomCell.m and add the following code

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

}

now go to tableViewController and just play with your labels

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

}

of course, change the image name//,.......regards

0

精彩评论

暂无评论...
验证码 换一张
取 消