我有一个UITableView,它填充了一些解析的JSON twitter数据。目的是让用户选择一行,并将数据传递给一个modalViewController,在这种情况下是一个显示坐标和注释信息的地图。UITableView didSelectRowAtIndexPath推送最后从NSArray加载的数据,无论选择哪一行
在调试控制台中,我可以看到加载到每个可见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];
}
感谢大家指出需要重新解析我的委托方法中的数据!此解决方案完美工作......再次感谢! – blueHula 2012-01-05 00:54:59