UITableViewCell行意外地改变了它们的顺序

时间:2015-03-23 05:10:31

标签: ios objective-c uitableview custom-cell

我有一个视图,我需要创建扩展表。为此目的,我使用SKSTableView,视图如下所示

enter image description here

表重新加载后,行的顺序会发生变化,如下所示

enter image description here

标题是这里的行,其余数据是子行。此视图中的标签是固定的,但在右侧,所有字段都是以编程方式创建的。 cellForSubRowAtIndexPath内的代码如下所示

    UITableViewCell *cell = [tableView   dequeueReusableCellWithIdentifier:CellIdentifier];
        if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];

        // Make cell unselectable
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        CustomTextField* tf;
        UIButton *button = nil;
        UISegmentedControl *segment = nil;
        [cell.textLabel setFont:FONT_OPENSANS_LIGHT(14)];
        [cell.textLabel setTextColor:[UIColor whiteColor]];

        switch (indexPath.section) {
        //all fields area created here example for 1 of the field is
            tf = nameField_ = [self makeTextField:self.name placeholder:@"Sunil Joshi"];
                        [nameField_ setKeyboardType:UIKeyboardTypeDefault];
                        [cell addSubview:nameField_];


        }
       }


 - (CustomTextField *) makeTextField: (NSString*)text  placeholder:     (NSString*)placeholder  {
    CustomTextField *tf = [[CustomTextField alloc] init];
    tf.placeholder = placeholder ;
    tf.text = text;
    tf.font = FONT_OPENSANS_LIGHT(12);
    tf.adjustsFontSizeToFitWidth = YES;
    tf.enabled = NO;
    tf.textColor = [UIColor whiteColor];
    return tf;
}

请帮我找到这些行改变订单的原因。

3 个答案:

答案 0 :(得分:0)

如您的代码所示,您的单元格正在重复使用,并且您根据某些条件添加了UITextField

[cell addSubview:nameField_];

当正在重复使用单元格时,textField将显示在所有单元格中。要克服在所有行中显示textfield的问题,您需要从textField中删除此view,然后根据您的情况再次添加。

每次绘制单元格时,textField都会被添加到单元格中。

答案 1 :(得分:0)

如果不查看表创建的整个来源,很难弄清楚确切的问题。

但如果我要创建这样一个页面,那么我会遵循以下方法 -

1)创建一个UITableView实例,设置它的dataSource并委托给当前视图并将其添加到当前视图中。 2)创建个人详细信息和联系人详细信息

typedef enum {
    PERSONAL_DETAILS_SECTION = 0,
    CONTACT_DETAILS_SECTION,
} kListType;

#pragma mark section creation related methods

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2; // Personal Details and Contact Details
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50.0f;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return nil;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0 , 0, tableView.frame.size.width, 50.0)] autorelease];

    [view setBackgroundColor:[UIColor greenColor]];// set actual RGB

UIImageView *iconImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 30, 30)] autorelease];
            [imageView setImage:[UIImage imageNamed:((section == PERSONAL_DETAILS_SECTION) ? @"pdimage.png" : @"cdimage.png")]];
            [view addSubview:imageView];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake((5 *scalingFactor), (2 *scalingFactor), view.frame.size.width - (10 *scalingFactor), view.frame.size.height - (3 *scalingFactor))];
    label.text = ((section == PERSONAL_DETAILS_SECTION) ? @"Personal Details" : @"Contact Details");
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = NSTextAlignmentLeft;
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:(13.0f * scalingFactor)];

    [view addSubview:label];
    [label release];

    return view;
}

3)按如下方式创建单元格 -

#pragma mark cell creation related methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;// for both the section return one row 
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 0.0f;

    if(indexPath.section == PERSONAL_DETAILS_SECTION)
        height = 120.0;
    else
        height = 100.0;

    return height;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

     UITableViewCell *cell = nil;

    if(indexPath.section == PERSONAL_DETAILS_SECTION) 
{
    static NSString * cellId = @"personalDetailCellId";

    cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:cellId];

    if(cell == nil)
    {
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
           cell.selectionStyle = UITableViewCellSelectionStyleNone;

     // create instance of Name label and addict on the left side and it;s corresponding value label on then right side, customize, position and add them
     // create instance of Gender label on the left side and it's segment control on the right side, customize, position and add them
     // create instance of Age label on the left side and it's value label on the right side, customize, position and add them
     // create instance of Blood Group label on the left side and it's selection control on the right side, customize, position and add them
    }
    else
    {
    // cell is already created.
    }
  }
else // Contact Details Setion
{
    static NSString * cellId = @"contactDetailCellId";

    cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:cellId];

    if(cell == nil)
    {
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
           cell.selectionStyle = UITableViewCellSelectionStyleNone;

     // create instance of Mobile Number label on the left side and it's corresponding value label on right side, customize, position and add them
     // create instance of Landline No label on the left side and it's corresponding value label on right side, customize, position and add them
     // create instance of Email Id label on the left side and it's corresponding value label on right side, customize, position and add them

    }
    else
    {
    // cell is already created.
    }
  }

return cell;
}

注意:按照上述方法,您可以达到您的要求。此外,我刚刚编写了上述步骤,您需要编写相应注释的代码,并根据您的需要对其进行自定义。

答案 2 :(得分:0)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *CellIdentifier = @"someThingNew";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (!cell) {
                    cell = [[UITableViewCell alloc]init];
}

试试这个。