在分组的tableView中显示不同背景颜色的标签?
你好朋友。 我需要在UItableView中显示标签。 我怎样才能做到这一点。 请参阅截图。
你可以使用UITableViewCellStyleValue1
风格的UITableViewCell。 使用自定义视图的部分标题。
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 22.0f; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ UILabel *sectionLabel = [[UILabel alloc]initWithFrame:CGRectZero]; sectionLabel.backgroundColor = [UIColor purpleColor];//Choose your color sectionLabel.textColor = [UIColor whiteColor]; sectionLabel.font = [UIFont boldSystemFontOfSize:17.0f]; sectionLabel.text = @"Section Name"; return sectionLabel; } - (UITableViewCell *)tableView:(UITableView *)TableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; //Default detailTextLabel would have blue text color change it to your choice cell.detailTextLabel.textColor = [UIColor darkGrayColor]; } cell.textLabel.text = @"Mobile Number"; cell.detailTextLabel.text = @"Type"; return cell; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath { static NSString *CellsToBeReused = @"CellsToBeReused"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellsToBeReused]; if (cell == nil){ cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellsToBeReused] autorelease]; } UILabel* Label = [[UILabel alloc] initWithFrame:CGRectMake(2,2, 62, 23)]; [Label setText:@"Text"]; Label.backgroundColor = [UIColor whiteColor]; [cell.contentView addSubview:Label]; [Label release]; return cell; }
UITableViewDelegate有一个方法 –
(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
你可以简单地返回一个定制的UILabel
它更好地与UITableView彻底。 苹果是最好的资源
希望这可以帮助 !!!
UITableViewCell
对象有一个contentView
属性。 添加任何自定义视图作为contentView
subviews
。 你想要的是一个Custom UITableViewCell
。 如果你是谷歌,你会发现很多教程和信息。
例如 :
- 在IB中定制UITableViewCell 。
- 自定义UITableViewCell使用Interface Builder 。