当我添加新行时,标题行中的数据在新行中结束

时间:2014-01-07 03:56:25

标签: ios objective-c uitableview

我想通过添加按钮(+)添加新行 单击第二行中的添加按钮时(因为第一行是标题行) 新行必须具有相同的第二行数据

但是当我点击添加按钮时,第一行中的textlabel已在新行中结束

这是单元格初始

enter image description here

添加新行>>

enter image description here

在背景中的最后一个单元格中看到标题行中有标签

这是我的psudo代码: 在Viewdidload我初始化数据

dataTitleArray = [[NSMutableArray alloc] initWithObjects:@"", @"Ground Wood", @"Electronics", @"Floor", nil];
dataImageArray = [[NSMutableArray alloc] initWithObjects:@"", @"g1.jpeg", @"g2.jpeg", @"g3.jpeg",nil];
dataDetailsArray = [[NSMutableArray alloc] initWithObjects:@"subDetail1:100mm\nsubDetail2:100mL\nsubDetail3:100L", @"subDetail1:100mm\nsubDetail2:100mL\nsubDetail3:100L", @"subDetail1:100mm\nsubDetail2:100mL\nsubDetail3:100L",@"subDetail1:100mm\nsubDetail2:100mL\nsubDetail3:100L", nil];
表格中的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";


    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell ==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
    }   
    if(indexPath.row!=0){

        ImageAddButton = [UIButton buttonWithType:UIButtonTypeCustom ];
        [ImageAddButton addTarget:self action:@selector(operationMenu:)forControlEvents:UIControlEventTouchDown];
        ImageAddButton.tag =indexPath.row;
//        //[subButton setTitle:@"-" forState:UIControlStateNormal];
        ImageAddButton.frame = CGRectMake(5.0, 5.0, 120, 120.0);

        UIImage *Image = [UIImage imageNamed:[dataImageArray objectAtIndex:indexPath.row]];
        [ImageAddButton setImage:Image forState:UIControlStateNormal];
        [cell addSubview:ImageAddButton ];

        if (isAddPhoto) {
            ImageAddButton.hidden=true;
        }
        imageview = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 120, 120)];
        [cell addSubview:imageview];


       if(indexPath.row==_tagRowImage){
            imageview.image= self.photoImage;
       }        

        // add Details Label

        DetailsLabel.text = [dataDetailsArray objectAtIndex:indexPath.row];

        [cell addSubview:DetailsLabel];

        // add Title Label
        titleLabel.text = [dataTitleArray objectAtIndex:indexPath.row];

        [cell addSubview:titleLabel];

        //add minus button

        UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom
        [addButton addTarget:self action:@selector(addNewItem:)forControlEvents:UIControlEventTouchDown];

        UIImage *btnImage1 = [UIImage imageNamed:@"plus-6-m.png"];
        [addButton setImage:btnImage1 forState:UIControlStateNormal];
        addButton.frame = CGRectMake(270, 30.0, 30, 30.0);
        [cell addSubview:addButton];

        UIButton *subButton = [UIButton buttonWithType:UIButtonTypeCustom ];
        subButton.tag=indexPath.row;
        [subButton addTarget:self action:@selector(buttonWasPressed:) forControlEvents:UIControlEventTouchDown];
        subButton.frame = CGRectMake(270.0, 70.0, 30, 30.0);
        UIImage *btnImage2 = [UIImage imageNamed:@"minus-6-m.png"];
        [subButton setImage:btnImage2 forState:UIControlStateNormal];
        [cell addSubview:subButton];


    }else {

        [cell addSubview:titleText];
        [cell addSubview:reportText];
        [cell addSubview:companyText];
        [cell addSubview:dateText];
    }

    return cell;
}

1 个答案:

答案 0 :(得分:0)

dataTitleArray = [[NSMutableArray alloc] initWithObjects:@"", @"Ground Wood", @"Electronics", @"Floor", nil];
dataImageArray = [[NSMutableArray alloc] initWithObjects:@"", @"g1.jpeg", @"g2.jpeg", @"g3.jpeg",nil];
dataDetailsArray = [[NSMutableArray alloc] initWithObjects:@"subDetail1:100mm\nsubDetail2:100mL\nsubDetail3:100L", @"subDetail1:100mm\nsubDetail2:100mL\nsubDetail3:100L", @"subDetail1:100mm\nsubDetail2:100mL\nsubDetail3:100L",@"subDetail1:100mm\nsubDetail2:100mL\nsubDetail3:100L", nil];
表格中的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";


    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell ==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
    } 
    if ([cell.contentView subviews]){
      for (UIView *subview in [cell.contentView subviews]) {
        [subview removeFromSuperview];
     }
   }

    if(indexPath.row!=0){

        ImageAddButton = [UIButton buttonWithType:UIButtonTypeCustom ];
        [ImageAddButton addTarget:self action:@selector(operationMenu:)forControlEvents:UIControlEventTouchDown];
        ImageAddButton.tag =indexPath.row;
//        //[subButton setTitle:@"-" forState:UIControlStateNormal];
        ImageAddButton.frame = CGRectMake(5.0, 5.0, 120, 120.0);

        UIImage *Image = [UIImage imageNamed:[dataImageArray objectAtIndex:indexPath.row]];
        [ImageAddButton setImage:Image forState:UIControlStateNormal];
        [cell.contentView addSubview:ImageAddButton ];

        if (isAddPhoto) {
            ImageAddButton.hidden=true;
        }
        imageview = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 120, 120)];
        [cell.contentView addSubview:imageview];


       if(indexPath.row==_tagRowImage){
            imageview.image= self.photoImage;
       }        

        // add Details Label

        DetailsLabel.text = [dataDetailsArray objectAtIndex:indexPath.row];

        [cell.contentView addSubview:DetailsLabel];

        // add Title Label
        titleLabel.text = [dataTitleArray objectAtIndex:indexPath.row];

        [cell.contentView addSubview:titleLabel];

        //add minus button

        UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom
        [addButton addTarget:self action:@selector(addNewItem:)forControlEvents:UIControlEventTouchDown];

        UIImage *btnImage1 = [UIImage imageNamed:@"plus-6-m.png"];
        [addButton setImage:btnImage1 forState:UIControlStateNormal];
        addButton.frame = CGRectMake(270, 30.0, 30, 30.0);
        [cell.contentView addSubview:addButton];

        UIButton *subButton = [UIButton buttonWithType:UIButtonTypeCustom ];
        subButton.tag=indexPath.row;
        [subButton addTarget:self action:@selector(buttonWasPressed:) forControlEvents:UIControlEventTouchDown];
        subButton.frame = CGRectMake(270.0, 70.0, 30, 30.0);
        UIImage *btnImage2 = [UIImage imageNamed:@"minus-6-m.png"];
        [subButton setImage:btnImage2 forState:UIControlStateNormal];
        [cell.contentView addSubview:subButton];


    }else {

        [cell.contentView addSubview:titleText];
        [cell.contentView addSubview:reportText];
        [cell.contentView addSubview:companyText];
        [cell.contentView addSubview:dateText];
    }

    return cell;
}