在UITableView Objective-C中靠近每行添加标签颜色
我有一个在UITableView Objective-C中靠近每行添加标签颜色,objective-c,ios,uitableview,Objective C,Ios,Uitableview,我有一个UITableView,有不同的部分和行。我希望每个单元格都有不同的标签或图像,如图标,但我不知道为什么它看起来像这张图片: 滚动也改变了标签的大小 代码如下: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *key = [[self sectionKeys] objectAtIndex:[indexPat
UITableView
,有不同的部分和行。我希望每个单元格都有不同的标签或图像,如图标,但我不知道为什么它看起来像这张图片:
滚动也改变了标签的大小
代码如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
if(indexPath.row == 0)
{
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(3,0,40,40)];
label.backgroundColor = [UIColor darkGrayColor];
[cell.contentView addSubview:label];
}
else if(indexPath.row == 1)
{
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(3,0,40,40)];
label.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:label]; }
else if(indexPath.row == 2)
{
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(3,0,40,40)];
label.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:label]; }
else{
}
[[cell textLabel] setText:contentForThisRow];
return cell;
}
在您的代码中,当用户滚动表格时,标签创建了很多次!必须检查单元格中是否已存在颜色标签,然后设置颜色
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
int colorLabelTag = 9999;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Because table cells are reusing we must check is label already in cell
UILabel* label = (UILabel *) [cell viewWithTag:colorLabelTag];
// If no - we create label
if (!label) {
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(3,0,40,40)];
label.tag = colorLabelTag;
[cell addSubview:label];
}
if(indexPath.row == 0)
{
label.backgroundColor = [UIColor darkGrayColor];
}
else if(indexPath.row == 1)
{
label.backgroundColor = [UIColor redColor];
}
else if(indexPath.row == 2)
{
label.backgroundColor = [UIColor redColor];
}
[[cell textLabel] setText:contentForThisRow];
return cell;
在您的代码中,当用户滚动表格时,标签创建了很多次!必须检查单元格中是否已存在颜色标签,然后设置颜色
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
int colorLabelTag = 9999;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Because table cells are reusing we must check is label already in cell
UILabel* label = (UILabel *) [cell viewWithTag:colorLabelTag];
// If no - we create label
if (!label) {
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(3,0,40,40)];
label.tag = colorLabelTag;
[cell addSubview:label];
}
if(indexPath.row == 0)
{
label.backgroundColor = [UIColor darkGrayColor];
}
else if(indexPath.row == 1)
{
label.backgroundColor = [UIColor redColor];
}
else if(indexPath.row == 2)
{
label.backgroundColor = [UIColor redColor];
}
[[cell textLabel] setText:contentForThisRow];
return cell;
除非您想要更多的Cusotomization,否则没有必要对单元格进行子类化。 让我试一试:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
// If you need to add more custom views then create them here. E.g.:
UILabel *myLable = [[UILabel alloc] initWithFrame:CGRectMake(10,200,20,20)];
myLable.text = @"Some Value";
label.tag = 101; // use anything but 1. Better use a constant expression.
[cell.contentView addSubview:myLable];
}
switch (indexPath.row) {
case 1:
cell.imageView.image = [UIImage imageNamed:@"red.png"];
break;
case 2:
cell.imageView.image = [UIImage imageNamed:@"blue.png"];
break;
case 3:
cell.imageView.image = [UIImage imageNamed:@"green.png"];
break;
}
UILabel* theLabel = (UILabel *) [cell viewWithTag:101]; // again, a constant is of better style here.
if (theLabel { // it must exist. However, it is good practice to check upon that.
label.text = @"someOtherValue";
}
[[cell textLabel] setText:contentForThisRow];
// you may need to layout your subviews here.
return cell;
}
除非您想要更多的Cusotomization,否则没有必要对单元格进行子类化。 让我试一试:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
// If you need to add more custom views then create them here. E.g.:
UILabel *myLable = [[UILabel alloc] initWithFrame:CGRectMake(10,200,20,20)];
myLable.text = @"Some Value";
label.tag = 101; // use anything but 1. Better use a constant expression.
[cell.contentView addSubview:myLable];
}
switch (indexPath.row) {
case 1:
cell.imageView.image = [UIImage imageNamed:@"red.png"];
break;
case 2:
cell.imageView.image = [UIImage imageNamed:@"blue.png"];
break;
case 3:
cell.imageView.image = [UIImage imageNamed:@"green.png"];
break;
}
UILabel* theLabel = (UILabel *) [cell viewWithTag:101]; // again, a constant is of better style here.
if (theLabel { // it must exist. However, it is good practice to check upon that.
label.text = @"someOtherValue";
}
[[cell textLabel] setText:contentForThisRow];
// you may need to layout your subviews here.
return cell;
}
您的分区条件在哪里?只需通过[cell.imageview setBackgroundColor:blackColor]@NikhilBansal,但不是imageviewok,然后使用此[cell.textLabel setBackgroundColor:blackColor];滚动没有必要改变标签的大小,但显然改变了子视图的顺序?标准标签似乎位于40/40彩色标签的顶部。你为什么要用一个UI标签来显示这个页面呢?您可以简单地使用一个空UIView并相应地设置其背景颜色。您的分区条件在哪里?只需传递[cell.imageview setBackgroundColor:blackColor]@NikhilBansal,但不是imageviewok,然后使用此[cell.textLabel setBackgroundColor:blackColor];滚动没有必要改变标签的大小,但显然改变了子视图的顺序?标准标签似乎位于40/40彩色标签的顶部。你为什么要用一个UI标签来显示这个页面呢?你可以简单地使用一个空的UIView并相应地设置它的背景颜色。但是在那之后,我想添加另一个标签,我需要定制。在我意识到仅仅在这里和那里添加标签并不是真的需要之前,我自己做了很多子类化。但是,子类化的优点是,您也可以将layoutSubviews方法子类化。如果您想添加标签,那么您可以按照sree charan刚才的建议添加到该标签中。您可以标记它并检查它是否存在。这是因为从屏幕上消失的单元格被重复使用。因此,他们可能已经有了这个标签。但是,为了支持更多的子视图,我会修改我的答案。但是在那之后,我想添加另一个标签,我需要定制。在我意识到仅仅在这里和那里添加标签并不是真的需要之前,我自己做了很多子类化。但是,子类化的优点是,您也可以将layoutSubviews方法子类化。如果您想添加标签,那么您可以按照sree charan刚才的建议添加到该标签中。您可以标记它并检查它是否存在。这是因为从屏幕上消失的单元格被重复使用。因此,他们可能已经有了这个标签。不过,为了支持更多的子视图,我将修改我的答案。