tablecell iOS中的重叠标签定制

tablecell iOS中的重叠标签定制,ios,Ios,我在表中有3个部分,所以我只想在第1部分(第2行和第3行)中为我的标题单元格赋值,这样标题就会出现在下一部分,并在每个单元格中跳转。帮我解决这个问题! 这是我的密码: -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Cell"; UITableViewC

我在表中有3个部分,所以我只想在第1部分(第2行和第3行)中为我的标题单元格赋值,这样标题就会出现在下一部分,并在每个单元格中跳转。帮我解决这个问题! 这是我的密码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        //------------ Customise lable and image in cell ----------------------
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        self.nameModuleLable = [[UILabel alloc] initWithFrame:CGRectMake(35, 5, 80, 30)];
        self.nameModuleLable.font = [UIFont systemFontOfSize:13];
        self.nameModuleLable.textColor = [UIColor whiteColor];
        self.nameModuleLable.tag = 1;

        self.nuberOfMessLable = [[UILabel alloc] initWithFrame:CGRectMake(200, 0, 40, 40)];
        self.nuberOfMessLable.font = [UIFont systemFontOfSize:13];
        self.nuberOfMessLable.textColor = [UIColor whiteColor];
        self.nuberOfMessLable.tag = 2;

        self.cellimageView = [[UIImageView alloc] init];
        self.cellimageView.frame = CGRectMake(8, 11, 20, 20);
        self.cellimageView.tag = 3;

        [cell addSubview:self.nameModuleLable];
        [cell addSubview:self.cellimageView];
        [cell addSubview:self.nuberOfMessLable];

    }
    self.nameModuleLable = (UILabel*)[cell viewWithTag:1];
    self.nuberOfMessLable = (UILabel*)[cell viewWithTag:2];
    self.cellimageView = (UIImageView*)[cell viewWithTag:3];

    // -------------Number of section in cell 2 and 3 in section 0-----------
    if (indexPath.section == 0) {
        if (indexPath.row == 2) {
            if (!numberOfMessage) {
                self.nuberOfMessLable.text = @"0";
            } else {
                self.nuberOfMessLable.text = [NSString stringWithFormat:@"%d",numberOfMessage];
            }
        }

        else if (indexPath.row == 3) {
            if (!numberOfFriend) {
                self.nuberOfMessLable.text = @"0";
            } else {
                self.nuberOfMessLable.text = [NSString stringWithFormat:@"%d",numberOfFriend];
            }
        }
        //-------------------Assign value for home's menu------------------------------
        self.nameModuleLable.text = [home objectAtIndex:indexPath.row];
        self.cellimageView.image = [UIImage imageNamed:[homeImage objectAtIndex:indexPath.row]];
        //--------------------------Assign value for application's menu--------------------------
    } else if (indexPath.section == 1){
        Module *currentModule = [self.moduleArray objectAtIndex:indexPath.row];
        self.nameModuleLable.text = [currentModule valueForKey:@"mName"];
        NSString *avaImageString = [currentModule valueForKey:@"mImage"];
        NSURL *urlAva = [NSURL URLWithString:avaImageString];
        NSData *dataAva = [NSData dataWithContentsOfURL:urlAva];
        self.cellimageView.image = [UIImage imageWithData:dataAva];
        //---------------------------Logout section---------------------------------
    } else if (indexPath.section == 2) {
        self.nameModuleLable.text = [logout objectAtIndex:indexPath.row];
        self.cellimageView.image = [UIImage imageNamed:[logoutImage objectAtIndex:indexPath.row]];
    }

    return cell;
}
以下是一些建议:

  • 创建UITableViewCell的自定义子类(如命名:CustomCell),其中包含nameModuleLable NuberOfMessable和cellimageView。在CustomCell的init方法初始化上述三个子视图

  • 更改tableView:cellForRowAtIndexPath:方法

        -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *CellIdentifier = @"Cell";
        CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            //------------ Customise lable and image in cell ----------------------
            cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    
    
        }
    
    
        // -------------Number of section in cell 2 and 3 in section 0-----------
        if (indexPath.section == 0) {
            if (indexPath.row == 2) {
                if (!numberOfMessage) {
                    cell.nuberOfMessLable.text = @"0";
                } else {
                    cell.nuberOfMessLable.text = [NSString stringWithFormat:@"%d",numberOfMessage];
                }
            }
    
            else if (indexPath.row == 3) {
                if (!numberOfFriend) {
                    cell.nuberOfMessLable.text = @"0";
                } else {
                    cell.nuberOfMessLable.text = [NSString stringWithFormat:@"%d",numberOfFriend];
                }
            }
            //-------------------Assign value for home's menu------------------------------
            cell.nameModuleLable.text = [home objectAtIndex:indexPath.row];
            cell.cellimageView.image = [UIImage imageNamed:[homeImage objectAtIndex:indexPath.row]];
            //--------------------------Assign value for application's menu--------------------------
        } else if (indexPath.section == 1){
            Module *currentModule = [self.moduleArray objectAtIndex:indexPath.row];
            cell.nameModuleLable.text = [currentModule valueForKey:@"mName"];
            NSString *avaImageString = [currentModule valueForKey:@"mImage"];
            NSURL *urlAva = [NSURL URLWithString:avaImageString];
            NSData *dataAva = [NSData dataWithContentsOfURL:urlAva];
            cell.cellimageView.image = [UIImage imageWithData:dataAva];
            //---------------------------Logout section---------------------------------
        } else if (indexPath.section == 2) {
            cell.nameModuleLable.text = [logout objectAtIndex:indexPath.row];
            cell.cellimageView.image = [UIImage imageNamed:[logoutImage objectAtIndex:indexPath.row]];
        }
    
        return cell;
    }
    
  • 为了给标题单元格赋值,还需要在ViewController中创建一些NSString来保存标题值


  • 它的拼写是“标签”:)请详细说明你想做什么,以及“让标题出现在下一节中,并在每个单元格中跳转”的含义是什么?对不起,我的英语水平^^!我的问题是,我仅在第0节中指定NumberOfMessable.text值,但它也出现在第1节中。将所有标签更改为标签:]每个单元格都有自己的子视图是最传统的方式。您可以查看苹果的示例代码: