gpt4 book ai didi

返回 TableView 单元格中指定类型的 UPnP 设备的 iOS 应用程序

转载 作者:行者123 更新时间:2023-11-29 13:03:59 25 4
gpt4 key购买 nike

我是 Objective-C 的中间人,我正试图让这个应用程序返回可以在本地网络中建立的某种类型的 UPnP 设备。我正在使用 UPnP Library这是我的代码:在 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];
}

此代码用于自定义表格 View 单元格的外观。

- (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;
}

我希望单元格只返回电灯开关设备的名称,谢谢!

编辑: 您好,我已经问了一个问题,但在这里很难。我提到的是:如何将存储在一行中的对象返回到先前创建的另一个对象?但我想在声明表格 View 单元格之前执行此操作。示例:

BasicUPnPDevice *device = [self.mDevices objectAtIndex:indexPath.row];

此处 *device 接收行中的对象。我想创建另一个数组来存储过滤后的设备,因此我可以通过这个新数组设置行数,而不是通过包含所有已创 build 备的数组设置行数。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [newArray count];
}

最佳答案

当您将设备添加到您的 mDevices 阵列时,您可以在此处过滤阵列或创建一个仅包含您希望在 TableView 中显示的设备的新阵列。然后在用于行计数 (numberOfRowsInSection) 和用于呈现 TableView 数据 (cellForRowAtIndexPath) 的 TableView 方法中,您将引用过滤数组中的对象。如果处理是异步的并且在 TableView 首次显示时数据不可用,则在数据可用时更新数组并调用方法重新加载表数据以显示最新数据。 [self.tableView reloadData];调用此方法将导致 tableview 再次执行 numberOfRowsInSection 和 cellForRowAtIndexPath 调用以访问过滤后的数组。 -rr

关于返回 TableView 单元格中指定类型的 UPnP 设备的 iOS 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19051941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com