简体   繁体   English

滚动时UITableviewCell问题

[英]UITableviewCell issue while scrolling

I Have created the table view dynamically, In cellForRowAtIndexPath i have added one custom button on cell as a subview 我动态创建了表视图,在cellForRowAtIndexPath中,我在单元格上添加了一个自定义按钮作为子视图

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: : 你不能在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 现在它解决了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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