宽容他人,放过自己。

搜索端口的实现

Posted on By anchoriteFili

搜索端口的实现

NSString *str = @"诛仙";
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *urlStr = [NSString stringWithFormat:@"http://api.dushubus.com/api/search?count=20&page=1&name=%@&uuid=564060195937280&",str];
    NSURL *url = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
   
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (data) {
            @autoreleasepool {
                self.names = [NSMutableArray array];
                NSDictionary *dic = [data objectFromJSONData];
                NSArray *arr = [dic objectForKey:@"content"];
                for (NSDictionary *contentDic in arr) {
                    [self.names addObject:[contentDic objectForKey:@"name"]];
                    NSLog(@"%@",[contentDic objectForKey:@"name"]);
                }
                [self.tableView reloadData];
            }
        }else {
                NSLog(@"为空");
            }
    }];

cellForRow的实现

SearchViewCell *searchCell = [tableView dequeueReusableCellWithIdentifier:@"search"];
    if (!searchCell) {
        searchCell = [[SearchViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"search"];
        searchCell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    searchCell.nameLabel.text = [self.names objectAtIndex:indexPath.row];
    
    return searchCell;