trouble using NSMUtableArray in UITableView
I have a NSMutableArray * nameCatalog which contains pair of names and urls(nam开发者_如何学Pythone, url).
If I display nameCatalog in viewDidLoad like this:
for (Catalogue *o in nameCatalog){
NSLog(@"Catalog: %@", o.url);
}
I get the following links, which is good:
Next I wanna put the NSMutableArray * nameCatalog as content to a table.And I implement the following:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSURL *url=[NSURL URLWithString:[[nameCatalog objectAtIndex:indexPath.row] valueForKey:@"url"]];
NSLog(@"the clicked url:%@", url);
}
So when I click the first row of the table it will be displayed the first url....the second and so on.
When I click first row it gets displayed this
which is correct.
When I click second row it displays this:
which is also correct.
When I click the third row it displays this:
(null)
which is WRONG!
Where I'm going wrong?
Has anything to do with the fact that the third link contains the french e?
in the word coupé.
IMPORTANT:
Above for simplisity and for make it clear for everyone I assumed that my NSMutableArray
contains only 3 urls.
In fact it contains much more, about 20 urls.And everything is going great except the urls that contain french e.When I click those rows of the table NSLog displays null.
And I'm sure that my NSMutableArray contains those links.Please tell me how to fix this?
Your 3rd string contains 'é' character that may be invalid in url - try to add percent escapes for that character using -stringByAddingPercentEscapesUsingEncoding: function in NSString:
NSString *escapedString = [[[nameCatalog objectAtIndex:indexPath.row] valueForKey:@"url"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:escapedString];
use this it work fine
NSArray *array=[[NSArray alloc] initWithObjects:@"link",@"link",@"link",nil];
for(int i=0;i<[array count];i++){
NSString *result = (NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)[array objectAtIndex:i], NULL, CFSTR("?=&+"), kCFStringEncodingUTF8);
NSURL *url=[NSURL URLWithString:result];
NSLog(@"the clicked url:%@", url);
NSLog(@"%@",[array objectAtIndex:i]);
}
加载中,请稍侯......
精彩评论