我已经使用 JSON/PHP/MYSQL 在表格视图中成功解析文本和图像。我只是将图像的位置存储在数据库中,而实际图像存储在我服务器上的目录中。与数据库中的图像相关的唯一存储是名称。示例汽车.jpg。我想要做的是在我的服务器上添加图像位置的 URL 前缀,这样就可以解析它们,而无需我进入数据库并手动输入 URL。这是我的一些代码...
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"studentsCell";
StudentsCell *cell = (StudentsCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StudentsCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSDictionary *studentsDict = [students objectAtIndex:indexPath.row];
//I want to prefix the URL for the key imagepath but i dont know where and how to do it.
NSURL *imageURL = [NSURL URLWithString:[studentsDict objectForKey:@"imagepath"]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *imageLoad = [[UIImage alloc] initWithData:imageData];
cell.imageView.image = imageLoad;
NSString *name = [NSString stringWithFormat:@"%@ %@", [studentsDict valueForKey:@"first"], [studentsDict valueForKey:@"last"]];
cell.title.text = name;
NSString *subtitle = [NSString stringWithFormat:@"%@", [studentsDict objectForKey:@"email"]];
cell.subtitle.text = subtitle;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(265, 6, 44, 44);
[button setImage:[UIImage imageNamed:@"email.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(email:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
// cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellbackground.png"]];
return cell;
}