我有一个包含两个单元格的tableview。现在这些单元格包含标签,我想更改这些标签的颜色。
我的代码:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CheckerCell";
CustomCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.titleLabel.text = [titleArray objectAtIndex:indexPath.row];
cell.subTitleLabel.text=[subTitleArray objectAtIndex:indexPath.row];
if (indexPath.row == 0) {
cell.textLabel.textColor = [UIColor colorWithRed:78/255.0 green:157/255.0 blue:19/255.0 alpha:1.0];
}
else if(indexPath.row == 1)
{
cell.textLabel.textColor = [UIColor colorWithRed:167/255.0 green:19/255.0 blue:43/255.0 alpha:1.0];
}
}
我在这里缺少什么?此代码也在基类中。这有关系吗?
请指导。
答案 0 :(得分:4)
您应该首先创建您的单元格:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if( !cell ) {
cell = [[UITableViewCell alloc] init];
if (indexPath.row == 0) {
cell.textLabel.textColor = [UIColor colorWithRed:78/255.0 green:157/255.0 blue:19/255.0 alpha:1.0];
}
else if(indexPath.row == 1)
{
cell.textLabel.textColor = [UIColor colorWithRed:167/255.0 green:19/255.0 blue:43/255.0 alpha:1.0];
}
}
}
同时结帐other functions也可以将单元格出列,并在此处查看最符合您需求的内容。
修改强>
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CheckerCell";
CustomCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// See this missing piece of code to create a cell
if( !cell ) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"SomeTableViewCellDesignedFromXIB" owner:nil options:nil] objectAtIndex:0];
}
// See this missing piece of code
cell.titleLabel.text = [titleArray objectAtIndex:indexPath.row];
cell.subTitleLabel.text=[subTitleArray objectAtIndex:indexPath.row];
if (indexPath.row == 0) {
cell.textLabel.textColor = [UIColor colorWithRed:78/255.0 green:157/255.0 blue:19/255.0 alpha:1.0];
}
else if(indexPath.row == 1)
{
cell.textLabel.textColor = [UIColor colorWithRed:167/255.0 green:19/255.0 blue:43/255.0 alpha:1.0];
}
}
答案 1 :(得分:2)
试试这个
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if (indexPath.row == 0) {
cell.textLabel.textColor = [UIColor colorWithRed:78/255.0 green:157/255.0 blue:19/255.0 alpha:1.0];
}
else if(indexPath.row == 1)
{
cell.textLabel.textColor = [UIColor colorWithRed:167/255.0 green:19/255.0 blue:43/255.0 alpha:1.0];
}
cell.textLabel.text = @"your text here";
return cell;
}
答案 2 :(得分:0)
您不是首先创建单元格,您需要这样做才能设置颜色
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString static * identifiderPlain = @"CellPlain";
UITableViewCell * cell;
cell = [tableView dequeueReusableCellWithIdentifier:identifiderPlain];
if(!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifiderPlain];
}
if (indexPath.row == 0) {
cell.textLabel.textColor = [UIColor colorWithRed:(78.0f/255.0f) green:(157.0f/255.0f) blue:(19.0f/255.0f) alpha:1.0f];
}
else if(indexPath.row == 1)
{
cell.textLabel.textColor = [UIColor colorWithRed:(167.0f/255.0f) green:(19.0f/255.0f) blue:(43.0f/255.0f) alpha:1.0f];
}
return cell;
}
答案 3 :(得分:-1)
使用此
cell.textLabel.backgroundColor = [UIColor colorWithRed:78/255.0 green:157/255.0 blue:19/255.0 alpha:1.0];