应用正在搜索相同范围内的信标。我在UTTableView中显示所有信标。我现在的问题是每次uitableview重新加载它都会创建一个新按钮。这使得应用程序非常慢并且需要非常大的CPU能力。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Tag"];
AXABeacon *beacon = [[AXABeacon alloc] init];
beacon = [self.bleDevices objectAtIndex:indexPath.row];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag = indexPath.row;
//set the position of the button
//NSLog(@"innan if %@", [self.setButton objectAtIndex:indexPath.row]);
NSLog(@"Skapar en ny ");
//NSLog(@"Inne i if %@", [self.setButton objectAtIndex:indexPath.row]);
button.frame = CGRectMake(cell.frame.origin.x + 190, cell.frame.origin.y + 7, 100, 30);
[button setTitle:@"Radera" forState:UIControlStateNormal];
[button addTarget:self action:@selector(deleteCell:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor= [UIColor clearColor];
[cell.contentView addSubview:button];
[self.setButton addObject:beacon];
NSLog(@"Efter if %@", [self.setButton objectAtIndex:indexPath.row]);
//Här är det slut på sätta knappar
//Definerar veriabler
UILabel *titleLabel = (UILabel *)[cell viewWithTag:1];
UILabel *uuidLabel = (UILabel *)[cell viewWithTag:2];
UILabel *rssiLabel = (UILabel *)[cell viewWithTag:3];
UILabel *majorLabel = (UILabel *)[cell viewWithTag:4];
UILabel *minorLabel = (UILabel *)[cell viewWithTag:5];
//Ger variabler ett värde
titleLabel.text = [NSString stringWithFormat:@"%@", beacon.name];
uuidLabel.text = [NSString stringWithFormat:@"%@", beacon.uuidString];
rssiLabel.text = [NSString stringWithFormat:@"RSSI: %@", [beacon.rssi stringValue]];
minorLabel.text = [NSString stringWithFormat:@"Minor: %@", beacon.minor];
majorLabel.text = [NSString stringWithFormat:@"Major: %@", beacon.major];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//Anpassar text till storlek av box
uuidLabel.adjustsFontSizeToFitWidth = YES;
titleLabel.adjustsFontSizeToFitWidth = YES;
return cell;
}
答案 0 :(得分:1)
每次出列单元格时,都会添加一个新按钮。解决这个问题的两种方法。
创建一个UITableViewCell
子类,用于在init
方法中创建和添加按钮。
更糟糕的解决方案:使用标记引用按钮。看起来你正在尝试使用按钮来引用它所在的行,所以无论如何你都不能这样做。您需要使用解决方案#1
答案 1 :(得分:0)
我认为这是因为你为每个按钮,标签和信标分配init。当细胞不存在时这样做。
if(!cell)
{
beacon = [[AXABeacon alloc] init];
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// and so on
}
希望这有帮助。