[英]iOS app that returns specified types of UPnP devices in a Table View cell
我是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];
}
當您將設備添加到mDevices陣列時,可以在此處過濾該陣列或創建僅包含您希望在表視圖中顯示的設備的新陣列。 然后在tableview方法中用於行計數(numberOfRowsInSection)和用於呈現tableview數據(cellForRowAtIndexPath),您將引用已過濾數組中的對象。 如果處理是異步的,並且在表視圖首次顯示時數據不可用,則在數據可用時更新數組,並調用一種方法來重新加載表數據以顯示最新數據。 [self.tableView reloadData]; 調用此方法將導致表視圖再次執行numberOfRowsInSection和cellForRowAtIndexPath調用以訪問您的過濾數組。 -rrh
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.