从UITableViewCell的子类返回自定义UILabel(编程)



我是iOS的新手,很抱歉我的英语不好。我想通过更改应用程序的外观主题UIAppearance代理。然后我有一些不同的单元格,它们是UITableViewCell的一个子类,每个单元格都有一个样式。一些单元格中的一些标签具有不同的颜色,因此单元格的" textLabel "属性可能在一个单元格中为白色,而在另一个单元格中为红色。

我有一个UILabel子类,例如,文本的红色。我改变了这个标签的外观颜色,如下所示:

[[MyRedLabel appearance] setTextColor:[UIColor redColor]];

我想使用相同的单元格所以我所做的是像这样覆盖方法:

-(UILabel*) textLabel {
   //(MyRedLabel is only put for test purposes, lastly I'll remove it by a property that        will be what I want)
   return [[MyRedLabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
}

问题是,当TableView绘制单元格时,文本不是。你能帮帮我吗?谢谢。

[编辑]

对不起,我没有解释。对于我想要的每种颜色,我都有一个UILabel的子类,所以如果我想要蓝色标签,我可以这样做:

[[MyBlueLabel appearance] setTextLabel:[UIColor blueColor]]; 

我想给单元格分配我想要的标签:

//Cell1
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Identifier"];
if (cell == nil) {
    cell = [[[MyCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel = [[MyBlueLabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
//...
//Cell2
//....  
cell.textLabel = [[MyRedLabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];

然后当我做cell.textLabel。text = @"Hi world";文本将在cell1为蓝色,在cell2为红色。

我想知道这是否正确因为我不想用。xib

创建4个或更多的自定义单元格

不清楚你在问什么。但是你不应该使用[MyReadLabel外观]来设置属性,除非你想让你所有的UILabel以同样的方式表现。如你所说的那样,我认为你滥用了这些方法。

最新更新