UITableView with live data also has user interaction same time
I'm working on proof of concept project. That is using a UITableView to display some live data. "live data" is beeing merging with sensor information on real time in the the app.
After UITableView rendered with some NSMutableArray data, that NSMutableArray is updating with sensor data appprox each 1 sec, also UITableView has user interaction same time.
My question is,开发者_开发技巧 according to above, what is the best approach consume live data on UITableView ?
Any suggestions, samples would be appreciated.
The SeismicXML parser sample code in the apple documentation does something similar. It gets 'live' (i.e. changing) information about earthquakes from a server and updates the datasource via an NSNotification when new information arrives. The UITableView reloads its data based on key-value observing of when the NSMutableArray changes. The reload is done with [tableView reloadData].
There is also user interaction with the table: the user can select a tableViewCell and choose to follow a link to further information.
If you're worried about controlling coordination:What you want to avoid is that the user taps on the table, the dataArray gets changed, and the -tableView:didSelectRowAtIndexPath:
method tries to access the data at the same time. There are two choices: either prevent selection in the table while the data updates or prevent the data from updating while the row selection was occurring.
You can prevent interaction with the table by setting tableView.allowsSelection=NO
. You can find out when the data is about to update by using the key-value-observing option: NSKeyValueObservingOptionPrior
. This approach generally loses points though because you may interrupt what the user wanted to do.
The other choice would be to delay the data update by setting up a couple of booleans: isSelecting and dataNeedsReloading. Flow would be something like:
the user taps the table, isSelecting=YES
If new data come in,
if (isSelecting) {
[tempArray addObject:newData];
dataNeedsReloading=YES;
}
Then when the selection process finishes, reset isSelecting=NO
, check the dataNeedsReloading
flag, combine the arrays and reload the table if needed. When finished, reset dataNeedsReloading=NO
. Also reset the tempArray with -removeAllObjects
.
If you want to update the UITableView
every time the datasource changes, send it a reload
message.
Call addData method on main thread whenever new data is available from Sensor.
-(void) addData:(NSString*)newDataFeild{
@synchronized(self){
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[_data count] inSection:0];
[_data addObject:line];
[self reloadData];
[self scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [_data objectAtIndex:indexPath.row];
return cell;
}
精彩评论