我是 Objective-C 的中间人,我正试图让这个应用程序返回可以在本地网络中建立的某种类型的 UPnP 设备。我正在使用UPnP 库,这是我的代码: 在 viewDidLoad 中,它使用已建立的 UPnP 对象初始化数组 mDevice。
- (void)viewDidLoad
{
[super viewDidLoad];
UPnPDB* db = [[UPnPManager GetInstance] DB];
self.mDevices = [db rootDevices]; //BasicUPnPDevice
[db addObserver:(UPnPDBObserver*)self];
//Optional; set User Agent
//[[[UPnPManager GetInstance] SSDP] setUserAgentProduct:@"upnpxdemo/1.0" andOS:@"OSX"];
//Search for UPnP Devices
[[[UPnPManager GetInstance] SSDP] searchSSDP];
}
节数只有一个
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
在这里,我相信这是我的问题。我不能只返回我需要的设备类型。它返回创建的所有设备的行数,我只想返回指定的设备,女巫是 BinaryLightDevice,我在下一秒的代码中从 XML 获取此信息。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [mDevices count];
}
此代码用于自定义表格视图单元格的外观。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
BasicUPnPDevice *device = [self.mDevices objectAtIndex:indexPath.row];
[[cell textLabel] setText:[device friendlyName]];
if([[device urn] isEqualToString:@"urn:schemas-upnp org:device:lightswitch:1"])
{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
我希望单元格只返回电灯开关设备的名称,谢谢!
EDIT:
你好,我已经问了一个问题,但在这里很难。我提到的是:如何将存储在一行中的对象返回给先前创建的另一个对象?但我想在声明表格视图单元格之前执行此操作。例子:
BasicUPnPDevice *device = [self.mDevices objectAtIndex:indexPath.row];
这里*device
接收行中的对象。我想创建另一个数组来存储过滤后的设备,所以我可以通过这个新数组而不是由包含所有已创建设备的数组来设置行数。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [newArray count];
}