如何在UITableViewCell中创建多个标签并为每个标签设置操作

时间:2016-03-31 12:36:18

标签: ios objective-c uitableview tags

我在UITableView Cell中的_ParentContentView子视图中创建了标签。下面的代码手动创建所有标签,并可能增加每个标签的内存,它的子视图

  • 我想为每个标签设置操作
  • 我喜欢在tableivewCell中使用滚动insdie的所有标签 表格视图单元格。平均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;
                }
    

    }

code output

2 个答案:

答案 0 :(得分:0)

你可以这样做。

  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];

            }
    }

答案 1 :(得分:0)

对于根据其内容设置行高,您应该设置tableview的两个属性

_tableViewOne.estimatedRowHeight = 50; //minimum row height
_tableViewOne.rowHeight = UITableViewAutomaticDimension;

所以根据它的内容它会占据行高。

  

不要使用:

     使用上述代码时的

heightForRowAtIndexPath方法。