2013-09-27 20 views
1

我是Objective-C的中介,我试图让此应用程序返回可以在本地创建的某种类型的UPnP设备网络。我使用的是UPnP Library,这是我的代码: 在viewDidLoad中,它使用建立的UPnP对象初始化数组mDevice。在表格视图单元中返回指定类型的UPnP设备的iOS应用程序

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

回答

1
当您将设备添加到您的mDevices阵列

,这是你会过滤阵列或创建只有你所关心的实现代码如下,以显示设备的一个新的数组。然后在行计数的tableview方法(numberOfRowsInSection)和呈现tableview数据(cellForRowAtIndexPath)中,您可以引用过滤数组中的对象。如果处理是异步的并且数据在表视图第一次显示时不可用,那么在数据可用时更新数组,并调用一个方法重新加载表数据以显示最近的数据。 [self.tableView reloadData];调用此方法将导致tableview再次执行numberOfRowsInSection和cellForRowAtIndexPath调用以访问您的已过滤数组。 -rrh

相关问题