2012-08-10 96 views
0

新的客观。我正在试图将2个值放入表格视图中。这是部分代码:如何将2个对象添加到tableview单元格

- (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]; 该表只不中的值增加对[alDescArray objectAtIndex:indexPath.row];

显示Alight Destination =哪部分我做错误请帮助

+0

你的标题应该是“我如何连接字符串”。可能重复http://stackoverflow.com/questions/510269/how-do-i-concatenate-strings-in-objective-c – 2012-08-10 06:22:25

回答

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; 

`

+0

感谢您的帮助 – sihao 2012-08-10 06:27:16

1

使用以下方法...

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

我认为这将是对你有所帮助。

+0

感谢您的帮助:) – sihao 2012-08-10 06:26:36

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 

在你的厦门国际银行,你可以加载CustomTableViewCell为你的类上观看的tableview

相关问题