将不同的图像添加到 UITableView 中单元格的 UIImageView
Add different images to UIImageView of cells in UITableView
不用说了,我是新手。我正在尝试将图像和文本添加到 UITableView。我已经能够添加文本,但在向所需文本添加不同图像时遇到问题。
我有 2 个单独的 PHP 代码用于 1 个数据库:
- 第一个获取文本和分配给文本的图像的 id (imageID)
- 第二个使用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];
}
table中的单元格:
- (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;
}
UITableView 的单元格是可重用的。这意味着不可见的单元格将在您重置之前显示相同的内容。
- (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];
...
}
只需添加重置代码。
您不应在 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"];
}
这将使您的申请运行更加顺利
不用说了,我是新手。我正在尝试将图像和文本添加到 UITableView。我已经能够添加文本,但在向所需文本添加不同图像时遇到问题。
我有 2 个单独的 PHP 代码用于 1 个数据库:
- 第一个获取文本和分配给文本的图像的 id (imageID)
- 第二个使用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];
}
table中的单元格:
- (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;
}
UITableView 的单元格是可重用的。这意味着不可见的单元格将在您重置之前显示相同的内容。
- (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];
...
}
只需添加重置代码。
您不应在 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"];
}
这将使您的申请运行更加顺利