2012-01-04 63 views
0

我有一个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]; 


} 

回答

0

您已经存储在您的viewController的财产results您的鸣叫资料,所以你只需要从那里获取数据和分析它再次(如Daryl Teo提到)在didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString* tweetText = [[self.results objectAtIndex:indexPath.row] objectForKey:@"text"]; 
    if ([tweetText rangeOfString:@" *** "].location != NSNotFound) { 

     NSArray *textItems = [tweetText componentsSeparatedByString:@" *** "]; 

     CallMapViewController *mapVC = [[CallMapViewController alloc] initWithNibName:@"CallMapViewController" bundle:[NSBundle mainBundle]]; 
     mapVC.callAddress = [[textItems objectAtIndex:0] stringByReplacingOccurrencesOfString:@" , " withString:@", "]; 
     mapVC.callType = [textItems objectAtIndex:1]; 

     [self presentModalViewController:mapVC animated:YES]; 
    } 
} 
+0

感谢大家指出需要重新解析我的委托方法中的数据!此解决方案完美工作......再次感谢! – blueHula 2012-01-05 00:54:59

0

你得到[indexPath行],但你不使用它。

而且你有“callType”和“callAddress”,它们不在委托方法的范围内。它们作为viewController的实例变量存在,并且在创建单元格时设置它们的值。这就是为什么它们的值始终是最后一个单元格的值。

您需要将数据存储在内存中,以便您可以在从indexPath获取该行时引用它。

NSInteger row = [indexPath row]; 
NSString *tweetText = [tweetsStorageArray objectAtIndex: row]; 

/* Parse Tweet Text again */ 

您可以将推文存储为文本,也可以创建存储类并将其存储。

0

从我可以在方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

看到你拿[indexPath row];但你不选择在您的数据基础。

0

我看到在你的委托- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath()你只能访问选定的行变量的NSLog消息。

你可能会需要使用UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]及其内容使用callType = cell.detailTextLabel.text;callAddress = cell.textLabel.text;接入小区

相关问题