在tableview中如何动态改变部分y位置ios

时间:2013-03-04 08:05:31

标签: ios uitableview dynamic

屏幕截图我在这里添加了动态数据。每个部分都有一行,并在其中包含标签和值。我的问题是当我添加或更改值的计数时,其部分的'y'边距会发生变化。如果有人知道,请帮帮我

这是我的代码

- (UIView *)tableView:(UITableView *)tableView1 viewForHeaderInSection:(NSInteger)section{

    UIView *section1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView1.bounds.size.width, tableView1.bounds.size.height)] ;
    [section1 setBackgroundColor:[UIColor clearColor]];


    return section1;

}

-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{

    if(section !=0){
        return 22;
    }
    return 0.0;
}
-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
{


    return 0.0;
}

//dynamic decide no of sections in table
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    if(tagLastbutton == lastRow){

        lastRow = lastRow +1;
        return lastRow;

    }
    return lastRow;

    //[self.arrayObjects count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return 1;
}



//  Return YES to allow the user to reorder table view rows
//
- (BOOL)tableView:(UITableView *)tableView
canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}


// Return a cell containing the text to display at the provided row index.
//changing background according to input
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"MyuSocialNetworkCell";
    CreateNewProfileViewCell * cell = (CreateNewProfileViewCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    if (cell == nil)
    {


        NSArray *nibObjects= [[NSBundle mainBundle] loadNibNamed:@"CreateNewProfileViewCell" owner:nil options:nil];

        for(id currentObject in nibObjects)
        {
            if([currentObject isKindOfClass:[CreateNewProfileViewCell class]])
            {
                cell = (CreateNewProfileViewCell *) currentObject;
                break;

            }
        }

    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    //[indexPath.row color]
   // if([self.subCategoryString length] != 0){
       // cell.addFoodAllergyLabel.text = self.subCategoryString;
   // }
    NSString *str;
    if(indexPath.section < [self.namesFoodSubCategoryLabels count]){
      str = [self.namesFoodSubCategoryLabels objectAtIndex:indexPath.section];
    }
    int len=str.length;

    NSLog(@"Ch ###########################%u",len);
    len=floor(len/29);
    NSLog(@"Ch ##After Row##################%u",len);
    len++;
   // len++;
    /*
    CGRect frameCell = CGRectMake(cell.frame.origin.x, cell.frame.origin.y+newCellPosition, cell.frame.size.width, cell.frame.size.height);
    cell.frame = frameCell;
    */

    CGRect frame = CGRectMake(cell.addFoodAllergyLabel.frame.origin.x, cell.addFoodAllergyLabel.frame.origin.y, cell.addFoodAllergyLabel.frame.size.width, cell.addFoodAllergyLabel.frame.size.height*len);
    cell.addFoodAllergyLabel.numberOfLines = len;
    cell.addFoodAllergyLabel.frame = frame;

    CGRect frameMidBox = CGRectMake(cell.midMainRowBoxImageView.frame.origin.x, cell.midMainRowBoxImageView.frame.origin.y, cell.midMainRowBoxImageView.frame.size.width, cell.midMainRowBoxImageView.frame.size.height*len);

    cell.midMainRowBoxImageView.frame = frameMidBox;

    CGRect frameLowerCornerBox = CGRectMake(cell.lowerRowCornerImageView.frame.origin.x, cell.lowerRowCornerImageView.frame.origin.y+((len*26)-26), cell.lowerRowCornerImageView.frame.size.width, cell.lowerRowCornerImageView.frame.size.height);

    newCellPosition = cell.lowerRowCornerImageView.frame.origin.y+22;

    cell.lowerRowCornerImageView.frame = frameLowerCornerBox;
    CGRect frameButtonImage = CGRectMake(cell.addFoodAllergyButton.frame.origin.x, cell.addFoodAllergyButton.frame.origin.y+((len * 13)-13), cell.addFoodAllergyButton.frame.size.width, cell.addFoodAllergyButton.frame.size.height);

    cell.addFoodAllergyButton.frame = frameButtonImage;
    cell.addFoodCategoryImageView.frame = frameButtonImage;
    NSLog(@"Array&&&&&&&&&&&&&&&&%@",self.namesFoodSubCategoryLabels);
    NSLog(@"indexPath.row %u",indexPath.row);

    if(indexPath.section < [self.namesFoodSubCategoryLabels count]){
        cell.addFoodAllergyLabel.text = [self.namesFoodSubCategoryLabels objectAtIndex:indexPath.section];
        cell.addFoodCategoryImageView.image = [UIImage imageNamed:[self.imagesFoodCategories objectAtIndex:indexPath.section]];



    }
    //CGRect frame = CGRectMake(0, 0, 60, 60);
    //cell.frame=frame;

    cell.addFoodAllergyButton.tag = indexPath.section+1;
    cell.addFoodAllergyLabel.tag  = indexPath.section+1;
    [cell.addFoodAllergyButton addTarget:self action:@selector(addFoodAllergyButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

-(CGFloat) tableView:(UITableView *) tableView1 heightForRowAtIndexPath:(NSIndexPath *)indexPath
{


    for(int i=0 ; i< [self.namesFoodSubCategoryLabels count] ; i++){


        NSString *str = [self.namesFoodSubCategoryLabels objectAtIndex:i];
        int len=str.length;

        NSLog(@"Ch ######heightForRow#####################%u",len);
        len=floor(len/29);
        len++;
        //len++;
        NSLog(@"Ch ##After heightForRow###################%u",len);
        return ((len*26)+11);


    }

    return 37;
}

我正在动态更改背景,标签和剖面高度,但无法动态处理剖面y。因此截图显示我的问题是管理y位置的部分边距,这是每两个部分之间的变化。

1 个答案:

答案 0 :(得分:0)

您可以根据某些条件更改部分的值,例如

args.Cancel = true;

因为部分从0,1,2,3,4,5,6 ...... n

开始