Iphone 将UILabel添加到自定义UITableViewCell
我正在uitableview中处理自定义单元格,并尝试在单元格中添加uilabels,但无法在其上设置文本。下面是我如何尝试添加单元格以及如何添加文本Iphone 将UILabel添加到自定义UITableViewCell,iphone,uikit,Iphone,Uikit,我正在uitableview中处理自定义单元格,并尝试在单元格中添加uilabels,但无法在其上设置文本。下面是我如何尝试添加单元格以及如何添加文本 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *MyIdentifier = @"MyIdentifier"; customCell *cell = (
- (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;
}
我的自定义单元格类:
- (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;
}
其他一切都很好,但我没有得到标签上的文字。标签本身正在从另一个uiview类调用
谢谢将标签上的文字设置为
cell.orangeText1.text = @"Mikes Bar";
cell.orangeText1.text = @"4.3 mi northeast";
cell.whiteText1.text = @"Level";
cell.whiteText1.text = @"Freshman";
正如Gyani所说,但你可以通过这样写来让它更客观:
[cell.orangeText1 setText:@“麦克风条”]代码>
更新
根据您的评论,这里有一个更新。
您需要确保在视图出现时设置标签文本。当它初始化时就不会了。尝试使用视图将显示
或单元格将显示
方法。执行此操作的标准方法是将UILabel作为单元格的属性公开。我强烈推荐这种方法
或者,您可以为whiteText1和orangeText1实现自定义设置器,然后在适当的标签上设置text属性。看起来您并没有在initWithStyle中保留对lblview的引用,所以您也需要这样做,以便可以重新引用它。(或者为标签指定一个标记,以便您可以使用该标记从内容视图中检索它。)orangeText1不是标签,而是nsstring,我正在将该字符串传递给自定义tableviewcell中的另一个视图。此视图最终具有标签。若要在UIlabel上设置文本,必须在cell.yourLabelName.text=@“StringYouWantDisplay”上方进行设置;