简体   繁体   中英

UITableviewCell issue while scrolling

I Have created the table view dynamically, In cellForRowAtIndexPath i have added one custom button on cell as a subview

if ([deviceNameArray count]!=0)
{
    pairButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    pairButton.frame = CGRectMake(230.0f, 10.0f, 70.0f, 25.0f);
    [pairButton setTitle:@"Connect" forState:UIControlStateNormal];
    pairButton.titleLabel.textColor = [UIColor lightGrayColor];
    pairButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    pairButton.titleLabel.font = [UIFont fontWithName:@"arial" size:10];

    pairButton.tag = indexPath.row;

    [pairButton addTarget:self action:@selector(connectToDevice:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:pairButton];
    isPairButtonAdded = YES;

}

I have changed the device the button title as disconnect after successful with my another Device. Now the problem is whenever am scrolling my table disconnect turning into connect but i dont want happen like that,i know its due to cell recreation how to stop recreating cell.

You must have to store record of connected and disconnected device in array and then you can manage your cell like this

if ([deviceNameArray count]!=0)
{
    pairButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    pairButton.frame = CGRectMake(230.0f, 10.0f, 70.0f, 25.0f);

    if([device isConnected]) // Get object from array and check is it coonected or disconnected
    {
       [pairButton setTitle:@"Connect" forState:UIControlStateNormal];
    }
    else
    {
        [pairButton setTitle:@"Disconnected" forState:UIControlStateNormal];
    }
pairButton.titleLabel.textColor = [UIColor lightGrayColor];
pairButton.titleLabel.textAlignment = NSTextAlignmentCenter;
pairButton.titleLabel.font = [UIFont fontWithName:@"arial" size:10];

pairButton.tag = indexPath.row;

[pairButton addTarget:self action:@selector(connectToDevice:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:pairButton];
isPairButtonAdded = YES;

}

You cannot do this in cellForRowAtIndexPath: :

// dequeue cell
if (!cell) {
    // create cell
}
// create button

Rather, you have to create the button inside the cell create block. Otherwise a new button will be created again each time the cell comes on-screen.

Just i got solved the issue, crating my button inside condition where i am checking

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

    if ([deviceNameArray count]!=0)
    {
        pairButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        pairButton.frame = CGRectMake(230.0f, 10.0f, 70.0f, 25.0f);
        [pairButton setTitle:@"Connect" forState:UIControlStateNormal];
        pairButton.titleLabel.textColor = [UIColor lightGrayColor];
        pairButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        pairButton.titleLabel.font = [UIFont fontWithName:@"arial" size:10];

        pairButton.tag = indexPath.row;

        [pairButton addTarget:self action:@selector(connectToDevice:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:pairButton];
        isPairButtonAdded = YES;

    }

now its solved

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM