简体   繁体   中英

how to add 2 object into a tableview cell

New to objective. I am trying to put 2 values into a table view. this is part of the code:

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

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.textLabel.font = [UIFont systemFontOfSize:14]; //Change this value to adjust size
    cell.textLabel.numberOfLines = 2;
    NSString *desc= @"Alight Destination =";
    cell.textLabel.text = desc,[alDescArray objectAtIndex:indexPath.row];    
    return cell;
}

for this line cell.textLabel.text = desc,[alDescArray objectAtIndex:indexPath.row]; the table only display Alight Destination = without adding in the values for [alDescArray objectAtIndex:indexPath.row];

What part did i do wrongly pls help

You should use

cell.textLabel.text=[NSString stringWithFormate:@"%@ %@",desc,[alDescArray objectAtIndex:indexPath.row];

or

NSString *desc=[NSString stringWithFormart: @"Alight Destination = %@",[alDescArray objectAtIndex:indexPath.row] ];
cell.textLabel.text = desc; 

`

Use following way...

cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",desc,[alDescArray objectAtIndex:indexPath.row]];

I think it will be helpful to you.

guess subclassing UITableViewCell will be useful. find code below hope it will help..!

# INTERFACE FILE

#import <UIKit/UIKit.h>

@interface CustomTableViewCell : UITableViewCell {  
    UIImageView *imageView;
    UILabel *titleLabel1;
    UILabel *titleLabel2;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;
- (void)setValuesForCell:(YourModal*)agent withIndexPath:(NSIndexPath*)indexPath;

@property(nonatomic,retain) UIImageView *imageView;
@property(nonatomic,retain) UILabel *titleLabel1;
@property(nonatomic,retain) UILabel *titleLabel2;

@end

#IMPLEMENTATION FILE

@implementation CustomTableViewCell
@synthesize titleLabel1,titleLabel2,imageView;


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
    // Your initialisation if any.
    }
    return self;
}

- (void)setValuesForCell:(YourModal*)modal withIndexPath:(NSIndexPath*)indexPath{
    titleLabel1.text=modal.name;
    titleLabel2.text=modal.description;
    imageView.image=modal.image;

        //Other values you want set from modal
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state.
}

- (void)dealloc {
    [super dealloc];
    [titleLabel1 release];
    [titleLabel2 release];
//Other memory releases
}


@end

In your xib you can load the CustomTableViewCell as your class for viewing on tableview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM