У меня есть UITableView, который заполнен некоторыми проанализированными данными JSON в Твиттере.Намерение состоит в том, чтобы пользователь выбрал строку и передал данные в modalViewController, который в этом случае является картой, отображающей информацию о координатах и аннотациях.
В консоли отладки я могу видеть данные, загруженные в каждый видимый UITableViewCell, плюс первый экран за пределами экрана (последний загруженный).Когда я запускаю приложение и пытаюсь выбрать строку, независимо от того, какую строку я выбираю, данные из последней загруженной ячейки всегда являются данными, передаваемыми в modalViewController.
Я вошел в систему, чтобы убедиться, что выбрана правильная строка (она есть), но независимо от того, какая строка выбрана, последними загруженными данными всегда являются данные, которые передаются.Методы
#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSUInteger count = [self.results count];
return count > 0 ? count : 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ResultCellIdentifier = @"ResultCell";
static NSString *LoadCellIdentifier = @"LoadingCell";
NSUInteger count = [self.results count];
if ((count == 0) && (indexPath.row == 0)) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:LoadCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:LoadCellIdentifier];
cell.textLabel.textAlignment = UITextAlignmentCenter;
}
if (self.connection) {
cell.textLabel.text = @"Loading...";
} else {
cell.textLabel.text = @"Not available";
}
return cell;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ResultCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:ResultCellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont systemFontOfSize:14.0];;
}
UIImage *image = [UIImage imageNamed:@"medicaltag.png"];
cell.imageView.image = image;
// Begin UITableCell Data Formatting
NSDictionary *tweet = [self.results objectAtIndex:indexPath.row];
NSString* tweetText = [tweet objectForKey:@"text"];
if ([tweetText rangeOfString:@" *** "].location !=NSNotFound) {
NSArray *textItems = [tweetText componentsSeparatedByString:@" *** "];
NSLog(@"%@", textItems);
callAddress = [textItems objectAtIndex:0];
callAddress = [callAddress stringByReplacingOccurrencesOfString:@" , " withString:@", "];
callType = [textItems objectAtIndex:1];
NSLog(@"%@", callType);
NSLog(@"%@", callAddress);
NSString *latitude = [textItems objectAtIndex:2];
NSString *latStringPt1 = [[NSString alloc] init];
NSString *latStringPt2 = [[NSString alloc] init];
NSString *longitude = [textItems objectAtIndex:3];
longitude = [longitude stringByReplacingOccurrencesOfString:@"- " withString:@"-"];
NSString *lonStringPt1 = [[NSString alloc] init];
NSString *lonStringPt2 = [[NSString alloc] init];
int latStringLen = [latitude length];
int lonStringLen = [longitude length];
NSLog(@"The value of integer num is %i", latStringLen);
latStringPt1 = [latitude substringWithRange:NSMakeRange(0,latStringLen-6)];
latStringPt2 = [latitude substringFromIndex:latStringLen-6];
combinedLatString = [latStringPt1 stringByAppendingString:@"."];
combinedLatString = [combinedLatString stringByAppendingString:latStringPt2];
lonStringPt1 = [longitude substringWithRange:NSMakeRange(0,lonStringLen-6)];
lonStringPt2 = [longitude substringFromIndex:lonStringLen-6];
combinedLonString = [lonStringPt1 stringByAppendingString:@"."];
combinedLonString = [combinedLonString stringByAppendingString:lonStringPt2];
NSLog(@"%@", combinedLatString);
NSLog(@"%@", combinedLonString);
}
cell.textLabel.text = [NSString stringWithFormat:@"%@", callAddress];
cell.textLabel.font = [UIFont boldSystemFontOfSize:16];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", callType];
cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
return cell;
}
Теперь метод делегата
#pragma mark -
#pragma mark Table View Delegate Methods*
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
NSLog(@"%i", row);
CallMapViewController *mapVC = [[CallMapViewController alloc] initWithNibName:@"CallMapViewController" bundle:[NSBundle mainBundle]];
mapVC.annotCallType = callType;
mapVC.annotCallAddress = callAddress;
NSLog(@"%@", mapVC.annotCallType);
NSLog(@"%@", mapVC.annotCallAddress);
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *lat = [f numberFromString:combinedLatString];
NSNumber *lon = [f numberFromString:combinedLonString];
mapVC.annotLatCoord = lat;
mapVC.annotLonCoord = lon;
NSLog(@"%@", lat);
NSLog(@"%@", lon);
NSLog(@"%@", callType);
NSLog(@"%@", callAddress);
[self presentModalViewController:mapVC animated:YES];
}