2016-05-13 23 views
0

我是IOS編程中的新手,我嘗試將BLE外設存儲在tableView中。 我做的唯一事情是將其存儲在一個NSArray:有我的代碼:在tableview中存儲BLE外設目標C

-(void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI 
{ 

NSLog (@"Discovered peripheral: %@", [peripheral name]); 

NSLog (@"peripheral services before connected: %@, RSSI Value : %@",advertisementData, RSSI); 

NSLog(@"adversting data %@",[NSString stringWithFormat:@"%@",[advertisementData description]]); 
NSString *localName = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey]; 
NSArray *foundArray = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey];; 

[self.centralManager stopScan]; 
NSLog(@"Scanning stopped"); 
NSLog(@"foundArray is %@",foundArray); 
self.tblfound = [[UITableView alloc] initWithFrame:CGRectMake(0, 10, 320, 300) style:UITableViewStylePlain]; 
self.tblfound.dataSource = self; 
self.tblfound.delegate = self; 
[self.view addSubview:self.tblfound]; 
} 
    - (void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals 

{ 
    for (CBPeripheral *peripheral in peripherals) { 
     NSLog(@"Retrieved Peripheral %@", peripheral.name); 
    } 

} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // static NSString *tableIdentifier = @"BLEDeviceList"; 
    //CBPeripheral *peripheral = [self.BTLEDevices objectAtIndex:indexPath.row]; 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ; 

    cell.selectionStyle = UITableViewCellSelectionStyleGray; 
} 


cell.textLabel.text = [foundArray objectAtIndex:indexPath.row]; 

return cell; 
} 

我不知道我是正確的,但我沒有找到任何爲例來幫助我。如果您有任何鏈接或代碼段可以幫助我請不要猶豫。 非常感謝==)!

+0

我建議您先閱讀有關如何使用'UITableView'(已設置文本)的教程。然後,您可以添加與外設自己陣列的複雜性。 – Larme

回答

0

你foundArray聲明是錯誤的

聲明的.h

#import "Emergency.h" 

@interface LiveDataVC : UIViewController 
{ 
    NSMutableArray *foundArray; 
}