
嗨朋友们。我需要在 UItableView 中显示标签。我怎样才能做到这一点。请参考截图。
您可以使用UITableViewCellStyleValue1UITableViewCell 的样式。对节标题使用自定义视图。
- (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;
}
最好彻底使用 UITableView。苹果是最好的资源
希望这可以帮助 !!!
- (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
UITableViewCell对象具有contentView属性。subviews从.添加任何自定义视图contentView。你想要的是一个Custom UITableViewCell. 如果你用谷歌搜索,你会发现很多教程和信息。
例如 :