不必说,我是新手。我正在尝试将图像和文本添加到UitableView。我已经能够添加文本,但是我在为所需文本中添加不同的图像时遇到了问题。
我有2个数据库的2个单独的PHP代码:
- 第一个获得了分配给文本(ImageId) 的图像的文本和ID
- 第二个使用ID(从第一个PHP)获取图像
我的代码很好,但是目前只出现1个图像
我的问题是如何将每个图像分配给他们的文本?
我如何不为没有图像的文本分配图像,因为某些文本没有图像?
我的代码如下:
连接和数据下载:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// Create an array to store the locations
NSMutableArray *list = [[NSMutableArray alloc] init];
// Parse the JSON that came in
NSError *error;
jsonArray = [NSJSONSerialization JSONObjectWithData:downloadedData options:NSJSONReadingAllowFragments error:&error];
// Loop through Json objects, create question objects and add them to our questions array
for (int i = 0; i < jsonArray.count; i++)
{
jsonElement = jsonArray[i];
// Create a new cell object and set its props to JsonElement properties
if (![jsonElement [@"Thought"] isEqual:[NSNull null]])
{
NSString *listText =jsonElement [@"Thought"];
if ([listText length] > 0)
{
NSString *cellText = jsonElement[@"Thought"];
[list addObject:cellText];
NSLog(@"list Cell: %@", cellText);
}
if (![jsonElement [@"filename"] isEqual:[NSNull null]])
{
imageID = jsonElement[@"id"];
}
NSLog(@"Cell Image: %@", imageID);
}
}
[[self tableView1] reloadData];
}
表中的单元格:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleIdentifier = @"SimpleIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:simpleIdentifier];
}
cell.textLabel.text = [list objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont italicSystemFontOfSize:20];
if ([imageID length] > 0)
{
urlStr = [NSString stringWithFormat:@"http://database.name/getimage.php?id=%@", imageID];
NSURL *imageUrl = [NSURL URLWithString:urlStr];
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
UIImage *imageLoad = [[UIImage alloc] initWithData:imageData];
cell.imageView.image = imageLoad;
}
return cell;
}
您不应在tableview:cellForRowAtIndexPath
中留下任何情况。尝试此
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleIdentifier = @"SimpleIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:simpleIdentifier];
}
cell.textLabel.text = [list objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont italicSystemFontOfSize:20];
if ([imageID length] > 0)
{
urlStr = [NSString stringWithFormat:@"http://database.name/getimage.php?id=%@", imageID];
NSURL *imageUrl = [NSURL URLWithString:urlStr];
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
UIImage *imageLoad = [[UIImage alloc] initWithData:imageData];
cell.imageView.image = imageLoad;
}
else //I assume this is the case when a row does not contain an image right?
{
cell.imageView.image = nil; //Or the below line for better aestethics
//cell.imageView.image = [UIImage imageNamed:@"placeholderimage.png"];
}
return cell;
}
顺便说一句,每次触发 tableview:cellForRowAtIndexPath
,这都会发生NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
。据我所知,dataWithContentsOfURL:
是一个繁重的过程,并使UI失速直到下载图像。因此,最好一次下载一个特定图像并将其保存在NSMutableArray
或NSMutableDictionary
中。例如:
//in your .m
@interface ViewController ()
@property NSMutableDictionary * imageIdDictionary;
@end
@implementation ViewController
-(void)viewDidLoad
{
[super viewDidLoad];
imageIdDictionary = [[NSMutableDictionary alloc] init];
}
并将您的tableView:cellForRowAtIndexPath:
更改为:
//Added this. You need a second array just like `list` which has all the different imageID's stored
imageID = [list2 objectAtIndex:indexPath.row];
if ([imageID length] > 0) {
if([self.imageIdDictionary objectForKey:imageID]) {
cell.imageView.image = [self.imageIdDictionary objectForKey:imageID];
}
else {
urlStr = [NSString stringWithFormat:@"http://database.name/getimage.php?id=%@", imageID];
NSURL *imageUrl = [NSURL URLWithString:urlStr];
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
UIImage *imageLoad = [[UIImage alloc] initWithData:imageData];
cell.imageView.image = imageLoad;
[self.imageIdDictionary setValue:imageLoad forKey:imageID];
//added these two lines!!!!!!!
NSArray * array = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]];
[table reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade];
}
}
else {
cell.imageView.image = nil; //Or the below line for better aestethics
//cell.imageView.image = [UIImage imageNamed:@"placeholderimage.png"];
}
这将使您的应用程序运行得更加顺畅
uitaiteView的单元格是可重复使用的。这意味着不可见的单元将在重置之前显示相同的内容。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleIdentifier = @"SimpleIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:simpleIdentifier];
}
cell.textLabel.text = @"";
cell.imageView.image = nil;
cell.textLabel.text = [list objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont italicSystemFontOfSize:20];
...
}
只需添加重置代码。