[英]UITableviewCell issue while scrolling
我動態創建了表視圖,在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;
}
使用我的另一台設備成功后,我已將設備的按鈕標題更改為斷開連接。 現在問題是每當我滾動我的表斷開連接但我不想發生這樣的事情,我知道它由於細胞娛樂如何停止重建細胞。
您必須在陣列中存儲已連接和斷開連接的設備的記錄,然后您可以像這樣管理您的單元
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;
}
你不能在cellForRowAtIndexPath:
這樣做:
// dequeue cell
if (!cell) {
// create cell
}
// create button
相反,您必須在單元格創建塊內創建按鈕。 否則,每次單元格出現在屏幕上時,將再次創建一個新按鈕。
只是我解決了這個問題,在我正在檢查的條件下裝箱我的按鈕
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;
}
現在它解決了
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.