我想在章节中设置标签...如何在tableview中的不同部分cz中设置不同的标签我想要制作不同的部分,我在章节中使用标题...通过这样做我做了不同的部分正确但无法区分各部分之间的数据..任何人都可以帮助我
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
// create the parent view that will hold header Label
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0,-60,300,60)];
// create the label object
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.frame = CGRectMake(0,0,self.view.frame.size.width,60);
UIColor* mainColor = [UIColor colorWithRed:47.0/255 green:168.0/255 blue:228.0/255 alpha:1.0f];
headerLabel.backgroundColor = mainColor;
headerLabel.font = [UIFont boldSystemFontOfSize:18];
headerLabel.textAlignment = UITextAlignmentCenter;
//
// UILabel *firstSection = [[UILabel alloc] initWithFrame:CGRectZero];
// firstSection.frame = CGRectMake(0,0,self.view.frame.size.width,60);
//
// firstSection.font = [UIFont boldSystemFontOfSize:18];
UIView *firstSection = [[UIView alloc] initWithFrame:CGRectMake(0,-60,300,60)];
//headerLabel.textAlignment = UITextAlignmentCenter;
if(section == 0)
headerLabel.text = @"Reciever Party";
UILabel *nameLbl =[[UILabel alloc]initWithFrame:CGRectMake(20, 35, 100 ,50)];
nameLbl.text =@"Name";
nameLbl.textColor=[UIColor blackColor];
UIFont *bold = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
[nameLbl setFont:bold];
nameLbl.backgroundColor =[UIColor clearColor];
[headerLabel addSubview:nameLbl];
UILabel *addLbl =[[UILabel alloc]initWithFrame:CGRectMake(20, 65, 100 ,50)];
addLbl.text =@"Address";
addLbl.textColor=[UIColor blackColor];
UIFont *bold1 = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
[addLbl setFont:bold1];
addLbl.backgroundColor =[UIColor clearColor];
[headerLabel addSubview:addLbl];
if(section == 1)
headerLabel.text = @"Sender Party";
UILabel *senderName =[[UILabel alloc]initWithFrame:CGRectMake(20, 125, 100 ,50)];
senderName.text =@"Address";
senderName.textColor=[UIColor blackColor];
UIFont *bold2 = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
[addLbl setFont:bold2];
senderName.backgroundColor =[UIColor clearColor];
[headerLabel addSubview:senderName];
if(section == 2)
headerLabel.text = @"Third Section Header";
headerLabel.textColor = [UIColor whiteColor];
[customView addSubview:headerLabel];
return customView;
}
答案 0 :(得分:2)
我给你一个带有硬编码数据的样本,你怎么能在sectionView中显示数据。 viewForHeaderInSection 负责创建每个部分的标题,而不是显示部分行中的数据。
我在单元格的内容视图中添加标签,因为您不想从情节提要/原型单元格添加此内容。
试试这个: -
在 cellForRowAtIndexPath
中- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YourIdentifier"];
/*
* If the cell is nil it means no cell was available for reuse and that we should
* create a new one.
*/
if (cell == nil) {
/*
* Actually create a new cell (with an identifier so that it can be dequeued).
*/
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"YourIdentifier"];
UILabel *lbl =[[UILabel alloc]initWithFrame:CGRectMake(20, 15, 100 ,50)];
lbl.textColor=[UIColor blackColor];
UIFont *bold1 = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
[lbl setFont:bold1];
lbl.hidden = NO;
lbl.tag = 101;
lbl.backgroundColor =[UIColor clearColor];
[cell.contentView addSubview:lbl];
}
UILabel *lblCell = (UILabel*)[cell.contentView viewWithTag:101];
if(indexPath.section == 0) //first section
{
if (indexPath.row == 0) //first row of first section
{
lblCell.text =@"Name";
}
if (indexPath.row == 1) //second row of first section
{
lblCell.text =@"Address";
}
}
if(indexPath.section == 1) //second section
{
if (indexPath.row == 0) //first row of second section
{
lblCell.text =@"age";
}
if (indexPath.row == 1) //second row of second section
{
lblCell.text =@"phone no.";
}
}
if(indexPath.section == 2) //third section
{
if (indexPath.row == 0) //first row of third section
{
lblCell.text =@"city";
}
if (indexPath.row == 1) //second row of third section
{
lblCell.text =@"father";
}
}
/* Now that the cell is configured we return it to the table view so that it can display it */
return cell;
}
答案 1 :(得分:1)
你必须像那样使用
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 40;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *sectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 225, 40)];
UIImageView *sectionImage = [[UIImageView alloc]initWithFrame:CGRectMake(5, 0, 40, 40)];
sectionImage.image = [UIImage imageNamed:[objMenuImageList objectAtIndex:section]];
sectionImage.contentMode = UIViewContentModeCenter;
[sectionView addSubview:sectionImage];
UILabel *sectionLable = [[UILabel alloc]initWithFrame:CGRectMake(50, 10, 150, 20)];
sectionLable.contentMode = UIViewContentModeLeft;
sectionLable.text = [objMenuList objectAtIndex:section];
[sectionLable setFont:[UIFont systemFontOfSize:14]];
sectionLable.textColor = [UIColor colorWithRed:(206/255.f) green:(180/255.f) blue:(116/255.f) alpha:1.0f];
[sectionView addSubview:sectionLable];
UIButton *sectionButton = [UIButton buttonWithType:UIButtonTypeCustom];
sectionButton.frame = CGRectMake(0, 0, sectionView.frame.size.width, sectionView.frame.size.height -10);
sectionButton.center = sectionView.center;
sectionButton.tag = section;
// sectionButton.backgroundColor = [UIColor yellowColor];
[sectionButton addTarget:self action:@selector(ExpandCell:) forControlEvents:UIControlEventTouchUpInside];
[sectionView addSubview:sectionButton];
sectionView.backgroundColor = [UIColor clearColor];
return sectionView;
}
答案 2 :(得分:0)
保持简单灵活。 这样做..
步骤1.为标题视图添加一个自定义单元格,标题为UILabel
。
步骤2.在cellForRowAtIndexPath
,dequeueReusableCellWithIdentifier
此单元格中。
将条件用作......
` if(section==0){
Cell.title.text= @"your title for section 0"
} // here title is UILabel `
通过这样做,您的代码足够灵活,可以进行任何未来的更改!!!