使用IB加载数据的UITableViewCell自定义单元格

从我的另一个问题 在UITableView中使用UITableViewCell我已经使用IB来创build自定义单元格。 下面是我的代码是如何:

ViewRoutes(原始表)

.h @interface ViewRoutes : UIViewController { IBOutlet UITableView *tblRoute; } @property(nonatomic, retain) UITableView *tblRoute; @end 

.M

  #import "ViewRoutes.h" #import "ViewRoutesCell.h" @implementation ViewRoutes @synthesize tblRoute; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell * aCell = [tableView dequeueReusableCellWithIdentifier:@"ViewRoutesCell"]; if (aCell == nil) { NSArray *arr = [[NSBundle mainBundle] loadNibNamed:@"ViewRoutesCell" owner:self options:nil]; for (NSObject *anObj in arr) { if([anObj isKindOfClass:[UITableViewCell class]]) { aCell = (UITableViewCell *)anObj; } return aCell; } } } 

而.xib只有一个UITableView就可以了。

ViewRoutesCell(我想成为自定义单元格)

。H

 #import <UIKit/UIKit.h> @interface ViewRoutesCell : UITableViewCell { IBOutlet UITableViewCell *routesCell; NSMutableArray *arryRouteText; NSMutableArray *arryRouteImage; IBOutlet UILabel *lblRouteText; IBOutlet UILabel *lblRouteImage; } @property (nonatomic, retain) NSMutableArray *arryRouteText; @property (nonatomic, retain) NSMutableArray *arryRouteImage; @property (nonatomic, retain) UILabel *lblRouteText; @property (nonatomic, retain) UILabel *lblRouteImage; @end 

我在自定义单元格中完成的唯一的事情就是合成这些项目

然后我定制的单元xib我有:

在这里输入图像说明

在这里输入图像说明

在这里输入图像说明

从这里我得到一个卡住,我不能解决如何从我的ViewRoutes.m设置这两个标签属性(他们将最终来自xml,但现在只是一个mutablearray)

我是这样做的吗?

汤姆

编辑只是为了让你知道我现在正在加载图像string到标签,稍后将是一个图像

在声明中

 UITableViewCell * aCell = [tableView dequeueReusableCellWithIdentifier:@"ViewRoutesCell"]; 

你正在创build一个UITableViewCell对象。 这不知道您的自定义标签。 您应该将其更改为:

 ViewRoutesCell * aCell = (ViewRoutesCell *)[tableView dequeueReusableCellWithIdentifier:@"ViewRoutesCell"]; 

那么,在if语句中,你应该改变

 aCell = (UITableViewCell *)anObj; 

 aCell = (ViewRoutesCell *)anObj; 

这将使编译器将该对象识别为您的特定单元格之一,从而允许您访问标签属性。

希望这可以帮助!

下面的代码不需要for循环replacefor循环

 acell.lblRouteText.text = [arryRouteText objectAtIndex:indexPath.row]; acell.lblRouteImage.text = [arryRouteImage objectAtIndex:indexPath.row]; return acell; 

正如你已经把返回语句放在For循环中,它总是使用第一个对象,其余的对象永远不会得到分配的机会。

而且,你不能分配图像标签,因为你将不得不使用UIImageView。

正如你已经定义了UILabel的属性,你必须在ViewRoutesCell.m文件中有@synthesized

现在,它很容易访问这些标签的属性。

在你的cellForRowAtIndexPath中 ,修改以下部分代码。

 for (NSObject *anObj in arr) { if([anObj isKindOfClass:[ViewRoutesCell class]]) { aCell = (UITableViewCell *)anObj; aCell.lblRouteText.text = @"My Route Text"; aCell.lblRouteImage.text = @"My Route Image"; } return aCell; } 

您应该将ViewRoutesCell类设置为IB单元作为文件的所有者,然后您可以将这些属性链接到IB文件