From my other question; Using UITableViewCell in a UITableView I've used the IB to create a custom cell. Heres how my code is at the moment:
ViewRoutes (Original Table)
.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;
}
}
}
and the .xib just has a UITableView on it.
The ViewRoutesCell (What I want to be the custom cell)
.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
The only thing i've done in the custom cell .m is synthesized the items
Then my custom cell xib i've got:
From here I get a little stuck, I can't work out how to set the two label properties from my ViewRoutes.m (they will be coming from xml eventually, but for now just a mutablearray)
Am I doing this the right way?
Tom
Edit Just to let you know i'm loading the image string to a label for now, will be an image later
In the declaration of
UITableViewCell * aCell = [tableView dequeueReusableCellWithIdentifier:@"ViewRoutesCell"];
you're creating a UITableViewCell
object. This does not know about your custom defined labels. You should change it to:
ViewRoutesCell * aCell = (ViewRoutesCell *)[tableView dequeueReusableCellWithIdentifier:@"ViewRoutesCell"];
Then, in the if
clause, you should change
aCell = (UITableViewCell *)anObj;
to
aCell = (ViewRoutesCell *)anObj;
This will make the compiler recognize the object as being one of your specific cells, allowing you to access the label properties.
Hope this helps!
As you have define property for UILabel, you must have @synthesized in ViewRoutesCell.m file.
Now, its easy to access properties of those labels.
In your cellForRowAtIndexPath , modify following part of code.
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;
}
There is no need of for loop replace your for loop by following code
acell.lblRouteText.text = [arryRouteText objectAtIndex:indexPath.row];
acell.lblRouteImage.text = [arryRouteImage objectAtIndex:indexPath.row];
return acell;
As you have put the return statement in For loop , it will always use the first object , and rest of the objects will never get a chance to get assigned.
And , you can not assign image to label , for that you will have to use UIImageView.
您应该将ViewRoutesCell类设置为IB单元作为文件的所有者,然后可以将这些属性链接到IB文件
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.