1

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

1 回答 1

1

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

于 2013-10-02T16:15:17.493 回答