Ios 如何在UITableViewCell内创建多个标记并为每个标记设置操作

Ios 如何在UITableViewCell内创建多个标记并为每个标记设置操作,ios,objective-c,uitableview,tags,Ios,Objective C,Uitableview,Tags,我正在UITableView单元格中的_ParentContentView子视图中创建标记。下面的代码手动创建所有标记,可能会增加每个标记及其子视图的内存 我希望每个标签都有动作 我喜欢将所有标签都放在tableivewCell中,而不在屏幕上滚动 表视图单元格。平均tableviewCell应增加/减少 内容大小自动调整 SpecialityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPa

我正在UITableView单元格中的_ParentContentView子视图中创建标记。下面的代码手动创建所有标记,可能会增加每个标记及其子视图的内存

  • 我希望每个标签都有动作
  • 我喜欢将所有标签都放在tableivewCell中,而不在屏幕上滚动 表视图单元格。平均tableviewCell应增加/减少 内容大小自动调整

    SpecialityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    int xPosition = 0;
    int yPosition = 0;  
    for (NSInteger buttonCount = 0; buttonCount<=dummyString.count-1; buttonCount++)
    {
    
    UIView *parentView = [[UIView alloc]init];
    parentView.frame = CGRectMake(10, 0, 150, 40);
    [parentView setFrame:CGRectMake(xPosition+10, yPosition,100, 25)];
    
    xPosition = xPosition+130;
    
    parentView.backgroundColor = [UIColor blackColor];
    parentView.alpha = 0.5;
    parentView.layer.cornerRadius = 15;
    
    UIImageView *childView =[[UIImageView alloc]init];
    childView.frame = CGRectMake(5, (parentView.frame.size.height-20)/2, 20, 20);
    childView.alpha = 1.0;
    childView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png",[appDelegate.topinImageArray objectAtIndex:3]]];
    childView.layer.cornerRadius = 10;
    [parentView addSubview:childView];
    UILabel *nameLabel = [[UILabel alloc] init];
    
     nameLabel.frame = CGRectMake(childView.frame.size.width+10, (parentView.frame.size.height-30)/2, 80, 30);
     nameLabel.text = [NSString stringWithFormat:@"%@", [dummyString objectAtIndex:buttonCount]];
     nameLabel.textColor = [UIColor whiteColor];
    [parentView addSubview:nameLabel];
    
    [cell._ParantContentView addSubview:parentView];
     if((buttonCount+1)%3==0)//4 means how many buttons you need for a row
                {
                    yPosition+= 50;
                    xPosition= 10;
                }
    
    SpecialityCell*cell=[tableView dequeueReusableCellWithIdentifier:@“cell”forIndexPath:indexPath];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    int xPosition=0;
    int-yPosition=0;
    对于(NSInteger buttonCount=0;buttonCount,您可以这样做

      label1 = (UILabel*)[cell.contentView viewWithTag:401];
            CGFloat widthLabelHeader = CGRectGetWidth(orderTable.frame)/7;
            if(!label1)
    
            {
    
                label1 = [[UILabel alloc]init];
                label1.frame = CGRectMake(0, 0, widthLabelHeader, 60);
                label1.layer.borderWidth = 0.5;
                label1.tag = 401;
                label1.font = [UIFont fontWithName:@"LaoSangamMN" size:14];
                [cell.contentView addSubview:label1];
            }
            label1.textAlignment = NSTextAlignmentCenter;
    
            label2 = (UILabel*)[cell.contentView viewWithTag:402];
            if(!label2)
    
            {
    
                label2 = [[UILabel alloc]init];
                label2.frame = CGRectMake(CGRectGetMaxX(label1.frame), 0, widthLabelHeader, 60);
                label2.layer.borderWidth = 0.5;
                label2.tag = 402;
                label2.font = [UIFont fontWithName:@"LaoSangamMN" size:14];
                [cell.contentView addSubview:label2];
            }
            label2.textAlignment = NSTextAlignmentCenter;
    
    
            label3 = (UILabel*)[cell.contentView viewWithTag:403];
            if(!label3)
    
            {
    
                label3 = [[UILabel alloc]init];
                label3.frame = CGRectMake(CGRectGetMaxX(label2.frame), 0, widthLabelHeader, 60);
                label3.layer.borderWidth = 0.5;
                label3.tag = 403;
                label3.font = [UIFont fontWithName:@"LaoSangamMN" size:14];
                [cell.contentView addSubview:label3];
            }
            label3.textAlignment = NSTextAlignmentCenter;
    
    
            label4 = (UILabel*)[cell.contentView viewWithTag:404];
            if(!label4)
    
            {
    
                label4 = [[UILabel alloc]init];
                label4.frame = CGRectMake(CGRectGetMaxX(label3.frame), 0, widthLabelHeader, 60);
                label4.layer.borderWidth = 0.5;
                label4.tag = 404;
                label4.font = [UIFont fontWithName:@"LaoSangamMN" size:14];
                label4.numberOfLines = 4;
                [cell.contentView addSubview:label4];
            }
            label4.textAlignment = NSTextAlignmentCenter;
    
    
            label5 = (UILabel*)[cell.contentView viewWithTag:405];
            if(!label5)
    
            {
    
                label5 = [[UILabel alloc]init];
                label5.frame = CGRectMake(CGRectGetMaxX(label4.frame), 0, widthLabelHeader, 60);
                label5.layer.borderWidth = 0.5;
                label5.tag = 405;
                label5.font = [UIFont fontWithName:@"LaoSangamMN" size:14];
                [cell.contentView addSubview:label5];
            }
            label5.textAlignment = NSTextAlignmentCenter;
    
    
            label6 = (UILabel*)[cell.contentView viewWithTag:406];
            if(!label6)
    
            {
    
                label6 = [[UILabel alloc]init];
                label6.frame = CGRectMake(CGRectGetMaxX(label5.frame), 0, widthLabelHeader, 60);
                label6.layer.borderWidth = 0.5;
                label6.tag = 406;
                label6.font = [UIFont fontWithName:@"LaoSangamMN" size:14];
                [cell.contentView addSubview:label6];
            }
            label6.textAlignment = NSTextAlignmentCenter;
    
    
    
               UIButton *editBtn = (UIButton*)[cell.contentView viewWithTag:407];
                if(!editBtn)
                {
    
                editBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                editBtn.frame = CGRectMake(CGRectGetMaxX(label6.frame), 0, widthLabelHeader, 60);
    
                editBtn.layer.borderWidth = 2.5;
                [editBtn addTarget:self action:@selector(editAction:) forControlEvents:UIControlEventTouchUpInside];
                [editBtn setTintColor:[UIColor blackColor]];
                    editBtn.tag = 407;
    
                    [cell.contentView addSubview:editBtn];
    
                    UIImageView *editimg = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMinX(editBtn.frame),0, CGRectGetWidth(label6.frame), 60)];
                    [editimg setImage:[UIImage imageNamed:@"edit-01.png"]];
                    [cell.contentView addSubview:editimg];
                }
            dict1 = [arrayFiltered objectAtIndex:indexPath.row];
            if( dict1)
            {
    
                NSString *strName = [dict1 objectForKey:@"i_name"];
                label1.text = @"";
                label1.text = strName;
                label1.backgroundColor = [UIColor whiteColor];
                label1.numberOfLines = 2;
    
                label2.text = @"";
                label2.text = [dict1 objectForKey:@"i_code" ];
                label2.backgroundColor = [UIColor whiteColor];
                label2.numberOfLines = 2;
    
                 NSString *strCategory = [dict1 objectForKey:@"i_category"];
    
                NSArray *arrValuesTempCat = [strCategory componentsSeparatedByString:@","];
                NSMutableArray *arrKeyTempCat = [NSMutableArray new];
    
                 for (int i = 0; i < arrValuesTempCat.count; i++)
                {
                    NSString *strValue = [arrValuesTempCat objectAtIndex:i];
                    if ([[categoryDict allKeys] containsObject:strValue]) {
    
                        NSString *str = [categoryDict objectForKey:strValue];
                        if (str.length > 0)
                        {
                            [arrKeyTempCat addObject:str];
                        }
                    }
                }
                if ([arrKeyTempCat containsObject:@""])
                {
                    [arrKeyTempCat removeObject:@""];
                }
    
                if ([arrKeyTempCat containsObject:@" "])
                {
                    [arrKeyTempCat removeObject:@" "];
                }
    
                NSString *strCategoryKeysParam = nil;
                if (arrKeyTempCat.count == 1)
                {
                    strCategoryKeysParam = [arrKeyTempCat objectAtIndex:0];
                }
                else
                {
                    strCategoryKeysParam = [arrKeyTempCat componentsJoinedByString:@","];
                }
    
    
    
                label3.text= @"";
                label3.text = strCategoryKeysParam;
                label3.backgroundColor = [UIColor whiteColor];
                label3.numberOfLines = 2;
    
                NSString *strElements = [dict1 objectForKey:@"i_elements"];
                NSArray *arrKeysTemp = [strElements componentsSeparatedByString:@","];
                NSMutableArray *arrValuesTemp = [NSMutableArray new];
    
                for (int i = 0; i < arrKeysTemp.count; i++)
                {
                    NSString *strKey = [arrKeysTemp objectAtIndex:i];
                    if ([[elementsDict allKeys] containsObject:strKey]) {
                        NSString *strValue = [elementsDict objectForKey:strKey];
                        [arrValuesTemp addObject:strValue];
                    }
                }
    
    
                NSString *strValues = [arrValuesTemp componentsJoinedByString:@","];
                label4.text = @"";
                label4.text = strValues;
                label4.backgroundColor = [UIColor whiteColor];
    
    
                label5.text = @"";
                label5.text = [dict1 objectForKey:@"i_quantity_produced" ];
                label5.backgroundColor = [UIColor whiteColor];
    
                label6.text = @"";
                label6.text = [dict1 objectForKey:@"active_status" ];
                label6.backgroundColor = [UIColor whiteColor];
    
    
                [editBtn setTitle:@"" forState:UIControlStateNormal];
    
    
                NSString *idStr = [NSString stringWithFormat:@"%d",[[dict1 objectForKey:@"i_id"] intValue]];
                NSLog(@"ids : %@, dict : %@",idStr, dict1);
                  [editBtn addTarget:self action:@selector(editAction:) forControlEvents:UIControlEventTouchUpInside];
                editBtn.accessibilityIdentifier = idStr;
                NSString *strID = editBtn.accessibilityIdentifier;
                NSLog(@"str ID : %@",strID);
    
    
                editItemId = [[dict1 objectForKey:@"i_id"]intValue];
                NSLog(@"Edited Item : %d",editItemId);
    
    
                //   [appDelegate hideIndicator];
    
                }
        }
    
    label1=(UILabel*)[cell.contentView-viewWithTag:401];
    CGFloat-widthLabelHeader=CGRectGetWidth(orderTable.frame)/7;
    如果(!label1)
    {
    label1=[[UILabel alloc]init];
    label1.frame=CGRectMake(0,0,宽度LabelHeader,60);
    label1.layer.borderWidth=0.5;
    标签1.tag=401;
    label1.font=[UIFont fontWithName:@“Laosangamm”大小:14];
    [cell.contentView addSubview:label1];
    }
    label1.textAlignment=NSTextAlignmentCenter;
    label2=(UILabel*)[cell.contentView视图带标记:402];
    如果(!label2)
    {
    label2=[[UILabel alloc]init];
    label2.frame=CGRectMake(CGRectGetMaxX(label1.frame),0,widthLabelHeader,60);
    label2.layer.borderWidth=0.5;
    标签2.tag=402;
    label2.font=[UIFont fontWithName:@“Laosangamm”大小:14];
    [cell.contentView addSubview:label2];
    }
    label2.textAlignment=NSTextAlignmentCenter;
    label3=(UILabel*)[cell.contentView视图带标记:403];
    如果(!label3)
    {
    label3=[[UILabel alloc]init];
    label3.frame=CGRectMake(CGRectGetMaxX(label2.frame),0,widthLabelHeader,60);
    label3.layer.borderWidth=0.5;
    标签3.tag=403;
    label3.font=[UIFont fontWithName:@“Laosangamm”大小:14];
    [cell.contentView addSubview:label3];
    }
    label3.textAlignment=NSTextAlignmentCenter;
    label4=(UILabel*)[cell.contentView视图带标记:404];
    如果(!label4)
    {
    label4=[[UILabel alloc]init];
    label4.frame=CGRectMake(CGRectGetMaxX(label3.frame),0,widthLabelHeader,60);
    label4.layer.borderWidth=0.5;
    标签4.tag=404;
    label4.font=[UIFont fontWithName:@“Laosangamm”大小:14];
    label4.numberOfLines=4;
    [cell.contentView addSubview:label4];
    }
    label4.textAlignment=NSTextAlignmentCenter;
    label5=(UILabel*)[cell.contentView视图带标记:405];
    如果(!label5)
    {
    label5=[[UILabel alloc]init];
    label5.frame=CGRectMake(CGRectGetMaxX(label4.frame),0,widthLabelHeader,60);
    label5.layer.borderWidth=0.5;
    标签5.tag=405;
    label5.font=[UIFont fontWithName:@“LaoSangamMN”大小:14];
    [cell.contentView addSubview:label5];
    }
    label5.textAlignment=NSTextAlignmentCenter;
    label6=(UILabel*)[cell.contentView视图带标记:406];
    如果(!label6)
    {
    label6=[[UILabel alloc]init];
    label6.frame=CGRectMake(CGRectGetMaxX(label5.frame),0,widthLabelHeader,60);
    label6.layer.borderWidth=0.5;
    label6.tag=406;
    label6.font=[UIFont fontWithName:@“Laosangamm”大小:14];
    [cell.contentView addSubview:label6];
    }
    label6.textAlignment=NSTextAlignmentCenter;
    UIButton*editBtn=(UIButton*)[cell.contentView视图带标记:407];
    如果(!editBtn)
    {
    editBtn=[UIButton按钮类型:UIButtonTypeRoundRect];
    editbn.frame=CGRectMake(CGRectGetMaxX(label6.frame),0,widthLabelHeader,60);
    editBtn.layer.borderWidth=2.5;
    [editBtn addTarget:self action:@selector(editAction:)for ControlEvents:UIControlEventTouchUpInside];
    [editBtn setTintColor:[UIColor blackColor]];
    editBtn.tag=407;
    [cell.contentView addSubview:editBtn];
    UIImageView*editimg=[[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMinX(editBtn.frame),0,CGRectGetWidth(label6.frame),60)];
    [editimg setImage:[UIImage IMAGENAME:@“edit-01.png”];
    [cell.contentView addSubview:editimg];
    }
    dict1=[arrayFiltered objectAtIndex:indexath.row];
    如果(第1条)
    {
    NSString*strName=[dict1 objectForKey:@“i_name”];
    label1.text=@;
    label1.text=strName;
    label1.backgroundColor=[UIColor whiteColor];
    label1.numberOfLines=2;
    label2.text=@;
    label2.text=[dict1 objectForKey:@“i_代码”];
    label2.backgroundColor=[UIColor whiteColor];
    label2.numberOfLines=2;
    NSString*strCategory=[dict1 objectForKey:@“i_类别”];
    NSArray*arrValuesTempCat=[strCategory Components由字符串分隔:@“,”];
    NSMUTABLEARRY*arrKeyTempCat=[NSMUTABLEARRY new];
    对于(int i=0;i0)
    {
    [arrKeyTempCat addObject:str];
    }
    }
    }
    if([arrKeyTempCat containsObject:@“”)
    {
    [arrKeyTempCat removeObject:@”“;
    }
    
    _tableViewOne.estimatedRowHeight = 50; //minimum row height
    _tableViewOne.rowHeight = UITableViewAutomaticDimension;