为什么我不能把[objectAtIndex:indexPath.row]放在NSString上

时间:2011-03-25 21:33:17

标签: xcode uitableview

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedFriend = [friends objectAtIndex:indexPath.row];
//selectedFriend invalid summary
//Initialize the detail view controller and display it.
LeaveMViewController *LMController = [[LeaveMViewController alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
LMController.whosmess = selectedFriend;
[LMController release];
LMController = nil;

请帮助

NSMutableArray *friends;
NSMutableDictionary *listofFriends;


 - (void) parseXMLFileAtURL:(NSString *) URL{   
friends = [[NSMutableArray alloc] init];

NSURL *xmlURL = [NSURL URLWithString:URL];  
friendsParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[friendsParser setDelegate:self];
[friendsParser setShouldProcessNamespaces:NO];
[friendsParser setShouldReportNamespacePrefixes:NO];
[friendsParser setShouldResolveExternalEntities:NO];

[friendsParser parse];

}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSString * errorString = [NSString stringWithFormat:@"Unable to download story feed        from web site (Error code %i )", [parseError code]];
NSLog(@"error parsing XML: %@", errorString);

UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
   }

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName   namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{          
if ([elementName isEqualToString:@"Friend"]){
    listOfFriends = [[NSMutableDictionary alloc] init];
    [listOfFriends setObject:[attributeDict objectForKey:@"firstname"] forKey:@"firstname"];
    [listOfFriends setObject:[attributeDict objectForKey:@"user_id"] forKey:@"user_id"];        
    [friends addObject:[listOfFriends copy]];   
}

    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *MyIdentifier = @"MyIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}

// Set up the cell
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
[cell setText:[NSString stringWithFormat:@"%@, %@",[[friends objectAtIndex: storyIndex] objectForKey: @"firstname"],[[friends objectAtIndex: storyIndex] objectForKey: @"user_id"]]];
return cell;
    }

   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedFriend = @"hui";
who=[[NSString alloc] initWithFormat:@"ai bliat %&",[friends objectAtIndex:indexPath.row]];
// NSString *selectedFriend = [friends objectAtIndex:indexPath.row];
LeaveMViewController *LMController = [[LeaveMViewController alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
LMController.whosmess = selectedFriend;
[LMController release];
LMController = nil;

}

1 个答案:

答案 0 :(得分:2)

问题解决了

NSString *selectedFriend = [friends objectAtIndex:indexPath.row];

取代
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
NSString *selectedFriend =[NSString initWithFormat @"%@", [[friends objectAtIndex: storyIndex] objectForKey: @"firstname"]];