图片下载后 table
image on table after download
我在单元格上有表格视图和图像。
我在尝试 return heightForRowAtIndexPath(图像高度不同)
时遇到问题
这是我的 class,当我尝试向下滚动时它会崩溃。
“2016-07-07 21:44:16.990 xem.vn[1725:25586] * 由于未捕获的异常 'NSRangeException' 而终止应用程序,原因:'* -[__NSArrayM objectAtIndex:]: 索引 2 超出范围 [0 .. 1]'""
- (void)viewDidLoad {
[listImage addObject:@"https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/leisa_christmas_false_color.png?itok=Jxf0IlS4"];
[listImage addObject:@"https://pixabay.com/static/uploads/photo/2015/10/01/21/39/background-image-967820_960_720.jpg"];
[listImage addObject:@"https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/idcs1426.jpg?itok=Gc_-Q58L"];
[listImage addObject:@"https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/leisa_christmas_false_color.png?itok=Jxf0IlS4"];
[listImage addObject:@"https://pixabay.com/static/uploads/photo/2015/10/01/21/39/background-image-967820_960_720.jpg"];
[listImage addObject:@"https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/idcs1426.jpg?itok=Gc_-Q58L"];
[listImage addObject:@"https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/leisa_christmas_false_color.png?itok=Jxf0IlS4"];
[listImage addObject:@"https://pixabay.com/static/uploads/photo/2015/10/01/21/39/background-image-967820_960_720.jpg"];
[listImage addObject:@"https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/idcs1426.jpg?itok=Gc_-Q58L"];
imageHeights = [[NSMutableArray alloc]init];
[self setDefaultRowHeights];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [listImage count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"newTableViewCell";
newTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = (newTableViewCell *)[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil][0];
}
NSString *imageURL = listImage[indexPath.row];
__weak newTableViewCell *weakCell = cell;
__weak typeof(self) weakSelf = self;
[cell.NewsImageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageURL]]
placeholderImage:[UIImage new]
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
weakCell.NewsImageView.image = image;
NSInteger oldHeight = [imageHeights[indexPath.row] integerValue];
NSInteger newHeight = (int)image.size.height;
if (image.size.width != CGRectGetWidth(weakCell.NewsImageView.bounds)) {
CGFloat ratio = image.size.height / image.size.width;
newHeight = CGRectGetWidth(self.view.bounds) * ratio;
}
if (oldHeight != newHeight) {
imageHeights[indexPath.row] = @(newHeight);
[weakSelf.NewsTableView beginUpdates];
[cell layoutIfNeeded];
[weakSelf.NewsTableView endUpdates];
}
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"Error: %@\nFetching image with url: %@", error, request.URL);
}];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [imageHeights[indexPath.row] integerValue];
}
- (void)setDefaultRowHeights {
imageHeights = [NSMutableArray arrayWithCapacity:listImage.count];
for (int i = 0; i < listImage.count; i++) {
imageHeights[i] = @(self.NewsTableView.rowHeight);
}
}
感谢帮助!!
我认为崩溃是由于您的 imageHeights
和 listImage
的项目数量不同。在 viewDidLoad
是 运行 之后,您的 listImage
有 8 个项目,但 imageHeights
0 个项目,即使在 运行 之后 setDefaultRowHeights
。
imageHeights = [NSMutableArray arrayWithCapacity:listImage.count]
只说imageHeights会持有和listImage相同的计数对象,但imageHeights
中的计数仍然是0。
我不确定 self.NewsTableView
是否设置正确。假设是正确的,setDefaultRowHeights
应该这样写:
- (void)setDefaultRowHeights {
imageHeights = [NSMutableArray arrayWithCapacity:listImage.count];
for (int i = 0; i < listImage.count; i++) {
[imageHeights addObject:@(self.NewsTableView.rowHeight)];
}
}
我在单元格上有表格视图和图像。 我在尝试 return heightForRowAtIndexPath(图像高度不同)
时遇到问题这是我的 class,当我尝试向下滚动时它会崩溃。 “2016-07-07 21:44:16.990 xem.vn[1725:25586] * 由于未捕获的异常 'NSRangeException' 而终止应用程序,原因:'* -[__NSArrayM objectAtIndex:]: 索引 2 超出范围 [0 .. 1]'""
- (void)viewDidLoad {
[listImage addObject:@"https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/leisa_christmas_false_color.png?itok=Jxf0IlS4"];
[listImage addObject:@"https://pixabay.com/static/uploads/photo/2015/10/01/21/39/background-image-967820_960_720.jpg"];
[listImage addObject:@"https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/idcs1426.jpg?itok=Gc_-Q58L"];
[listImage addObject:@"https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/leisa_christmas_false_color.png?itok=Jxf0IlS4"];
[listImage addObject:@"https://pixabay.com/static/uploads/photo/2015/10/01/21/39/background-image-967820_960_720.jpg"];
[listImage addObject:@"https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/idcs1426.jpg?itok=Gc_-Q58L"];
[listImage addObject:@"https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/leisa_christmas_false_color.png?itok=Jxf0IlS4"];
[listImage addObject:@"https://pixabay.com/static/uploads/photo/2015/10/01/21/39/background-image-967820_960_720.jpg"];
[listImage addObject:@"https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/idcs1426.jpg?itok=Gc_-Q58L"];
imageHeights = [[NSMutableArray alloc]init];
[self setDefaultRowHeights];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [listImage count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"newTableViewCell";
newTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = (newTableViewCell *)[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil][0];
}
NSString *imageURL = listImage[indexPath.row];
__weak newTableViewCell *weakCell = cell;
__weak typeof(self) weakSelf = self;
[cell.NewsImageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageURL]]
placeholderImage:[UIImage new]
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
weakCell.NewsImageView.image = image;
NSInteger oldHeight = [imageHeights[indexPath.row] integerValue];
NSInteger newHeight = (int)image.size.height;
if (image.size.width != CGRectGetWidth(weakCell.NewsImageView.bounds)) {
CGFloat ratio = image.size.height / image.size.width;
newHeight = CGRectGetWidth(self.view.bounds) * ratio;
}
if (oldHeight != newHeight) {
imageHeights[indexPath.row] = @(newHeight);
[weakSelf.NewsTableView beginUpdates];
[cell layoutIfNeeded];
[weakSelf.NewsTableView endUpdates];
}
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"Error: %@\nFetching image with url: %@", error, request.URL);
}];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [imageHeights[indexPath.row] integerValue];
}
- (void)setDefaultRowHeights {
imageHeights = [NSMutableArray arrayWithCapacity:listImage.count];
for (int i = 0; i < listImage.count; i++) {
imageHeights[i] = @(self.NewsTableView.rowHeight);
}
}
感谢帮助!!
我认为崩溃是由于您的 imageHeights
和 listImage
的项目数量不同。在 viewDidLoad
是 运行 之后,您的 listImage
有 8 个项目,但 imageHeights
0 个项目,即使在 运行 之后 setDefaultRowHeights
。
imageHeights = [NSMutableArray arrayWithCapacity:listImage.count]
只说imageHeights会持有和listImage相同的计数对象,但imageHeights
中的计数仍然是0。
我不确定 self.NewsTableView
是否设置正确。假设是正确的,setDefaultRowHeights
应该这样写:
- (void)setDefaultRowHeights {
imageHeights = [NSMutableArray arrayWithCapacity:listImage.count];
for (int i = 0; i < listImage.count; i++) {
[imageHeights addObject:@(self.NewsTableView.rowHeight)];
}
}