如何将2个对象添加到tableview单元格中

时间:2012-08-10 06:12:28

标签: iphone objective-c ios uitableview xcode4.3

新目标。我试图将2个值放入表格视图中。这是代码的一部分:

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

此行cell.textLabel.text = desc,[alDescArray objectAtIndex:indexPath.row]; 该表仅显示Alight Destination =而不添加[alDescArray objectAtIndex:indexPath.row];

的值

我做错了什么部分请帮忙

3 个答案:

答案 0 :(得分:6)

你应该使用

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

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

`

答案 1 :(得分:1)

使用以下方式...

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

我认为这会对你有所帮助。

答案 2 :(得分:1)

猜测子类化UITableViewCell会很有用。找到下面的代码希望它会有所帮助..!

# 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

在你的xib中,你可以加载CustomTableViewCell作为你的课程,以便在tableview上查看