質問

I'm intermediary at Objective-C and I'm trying to make this app witch returns the UPnP devices of a certain type that can be founded in the local network. I'm using UPnP Library and here is my code: At viewDidLoad, it is initialized the array mDevice with the founded UPnP objects.

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

The number of sections is just one

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
     return 1;
}

And here I believe there is my problem. I can't return only the device type I need. It returns the number of rows of all device founded, I want to return only for the specified devices, witch is BinaryLightDevice and I get this information from XML on next second code.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     return [mDevices count];
}

This code is for customize the appearance of table view cells.

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

I want the cell to return only the name of light switch devices, thanks!

EDIT: Hello, I've asked a question but in a hard way here. What I mentioned is: How do I return an object stored at one row to another object created previously? But I want to do this before declare the table view cells. Example:

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

Here the *device receives the object at the row. I want to create another array to store filtered devices, so i can set the number of rows by this new array and not by the array witch contains all the devices founded.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [newArray count];
}
役に立ちましたか?

解決

when you add devices to your mDevices array, this is where you would filter the array or create a new array of only the devices you care to show in the tableview. then in the tableview methods for row counts (numberOfRowsInSection) and for presenting tableview data (cellForRowAtIndexPath) you would refer to objects in your filtered array. if the processing is asynchronous and the data isn't available when the table view first shows then update your array when the data is available and call a method to reload your table data to show the most recent data. [self.tableView reloadData]; calling this method will cause the tableview to perform the numberOfRowsInSection and cellForRowAtIndexPath calls again to access your filtered array. -rrh

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top