I am working on custom cell in uitableview and trying to add uilabels in the cell but not able to set text on it. Following is how i am trying to add the cell and also how i am adding the text
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
customCell *cell = (customCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[customCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
cell.imageView.image = [UIImage imageNamed:@"iTeam.png"];
}
cell.orangeText1 = @"Mikes Bar";
cell.orangeText1 = @"4.3 mi northeast";
cell.whiteText1 = @"Level";
cell.whiteText1 = @"Freshman";
return cell;
}
My Custom cell class:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
// Initialization code
//
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
view.backgroundColor = [UIColor blackColor];
view.opaque = YES;
self.backgroundView = view;
[view release];
cellImage = [[UIImageView alloc] init];
myLabel *lblview = [[myLabel alloc] initWithFrame:CGRectMake(self.imageView.frame.size.width+10, 0, 320, 20) OrangeText:orangeText1 WhiteText:whiteText1];
[self.contentView addSubview:lblview];
[lblview release];
lblview = [[myLabel alloc] initWithFrame:CGRectMake(self.imageView.frame.size.width+10, 22, 320, 20) OrangeText:orangeText2 WhiteText:whiteText2];
[self.contentView addSubview:lblview];
[lblview release];
UIImage *image = [UIImage imageNamed:@"acessory.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
self.accessoryView = button;
[button release];
UIImageView *imagevw = [[UIImageView alloc] initWithFrame:CGRectMake(0, 50, 320, 8)];
imagevw.image = [UIImage imageNamed:@"linee.png"];
[self addSubview:imagevw];
[imagevw release];
}
return self;
}
Everything else is working fine but 开发者_StackOverflow社区i am not getting the text on the label. The label itself is being called from another uiview class.
Thanks
The standard way of doing this is to expose your UILabels as properties of your cell. I would highly recommend this approach.
Alternatively, you could implement custom setters for whiteText1 and orangeText1 that then set the text property on the appropriate label. It doesn't look like you're keeping a reference to lblview in your initWithStyle, so you'd need to do this as well so you could refer back to it. (Or assign a tag to the label so you can retrieve it from your content view using the tag.)
set text on label as
cell.orangeText1.text = @"Mikes Bar";
cell.orangeText1.text = @"4.3 mi northeast";
cell.whiteText1.text = @"Level";
cell.whiteText1.text = @"Freshman";
As Gyani said, but you can make it more ObjectiveC by writing it like:
[cell.orangeText1 setText:@"Mikes Bar"];
UPDATE
Per your comment, here is an update.
You will need to make sure setting the labels text is done when the view appears. Not when it's initialized. Try to use viewWillAppear
or cellWillDisplay
methods.
精彩评论