UITableViewCell rows changing their order unexpectedly

 ̄綄美尐妖づ 提交于 2019-12-13 00:54:53

问题


I have a view in which I need to create expanded table. For that purpose I am using SKSTableView and the view is as given below

As soon as table gets re loaded the order of rows is changing as shown below

Heading are rows here and rest data are subrows. Labels in this views are fixed but on right hand side all fields are being created programmatically. Code inside cellForSubRowAtIndexPath is shown below

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

Please help me in finding the issue why these rows are changing their order.


回答1:


As shown in your code, your cell is being reused and you are adding UITextField based on some condition.

[cell addSubview:nameField_];

As the cell is being is reused, the textField is shown in all cells. To overcome from these problem of showing textfield in all rows, you need to remove this textField from view and add again based on your condition.

Every time the cell is drawn the textField is getting added in cell.




回答2:


It's difficult to figure out the exact problem without viewing the whole source of the table creation.

But If I was to create such a page then I would have followed the following approach -

1) Create an instance of UITableView, set it's dataSource and delegates to the current view and adding it over the current view. 2) Create Personal Details and Contact Details as sections

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) Create Cell's as follows -

#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;
}

Note : By following above approach you can achieve your requirements. Also I have just written steps above, you need to write the code for the corresponding comments and customize them as per your needs.




回答3:


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

Try with this.



来源:https://stackoverflow.com/questions/29203647/uitableviewcell-rows-changing-their-order-unexpectedly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!