我正在從plist添加圖像到我的單元格視圖。此外,我也有與使用網址保存到plist中的圖像相同的問題。他們是不同的大小。我想調整它們到一定的大小,以便它們看起來都一樣。我試圖使用:在tableview中設置添加到單元格的圖像的大小
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.clipsToBounds = YES;
它沒有工作。我嘗試了一堆其他的東西,但沒有成功。我查了一堆東西,找不到有用的東西。這裏是我的單元格行方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [[searchResults objectAtIndex:indexPath.row] valueForKey:@"city"];
cell.detailTextLabel.text = [[searchResults objectAtIndex:indexPath.row] valueForKey:@"state"];
cell.imageView.image = [UIImage imageNamed:[[self.searchResults objectAtIndex:indexPath.row] valueForKey:@"cityImage"]];
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.clipsToBounds = YES;
} else {
cell.textLabel.text = [[content objectAtIndex:indexPath.row] valueForKey:@"city"];
cell.detailTextLabel.text = [[content objectAtIndex:indexPath.row] valueForKey:@"state"];
// Construct the expected URL for the image.
NSURL *imageFileURL = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask][0];
imageFileURL = [imageFileURL URLByAppendingPathComponent:[[self.content objectAtIndex:indexPath.row] valueForKey:@"cityImage"] isDirectory:NO];
// Attempt to load the image at the above URL.
cell.imageView.image = [[UIImage alloc] initWithContentsOfFile:[imageFileURL path]];
if (!cell.imageView.image)
cell.imageView.image = [UIImage imageNamed:[[self.content objectAtIndex:indexPath.row] valueForKey:@"cityImage"]];
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.clipsToBounds = YES;
}
return cell;
}
對於我的生活,我無法弄清楚這一點。我知道這可能是一個簡單的解決方法,但我想不出別的。
編輯:
至於建議,我創建了一個自定義單元格,宣佈在那裏layoutSubviews,然後子類細胞如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
MyCostumCell *cell = (MyCostumCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MyCostumCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [[searchResults objectAtIndex:indexPath.row] valueForKey:@"city"];
cell.detailTextLabel.text = [[searchResults objectAtIndex:indexPath.row] valueForKey:@"state"];
cell.imageView.image = [UIImage imageNamed:[[self.searchResults objectAtIndex:indexPath.row] valueForKey:@"cityImage"]];
//cell.textLabel.textColor = [UIColor colorWithRed:153.0/255.0f green:0.0/255.0f blue:0.0/255.0f alpha:1];
} else {
cell.textLabel.text = [[content objectAtIndex:indexPath.row] valueForKey:@"city"];
cell.detailTextLabel.text = [[content objectAtIndex:indexPath.row] valueForKey:@"state"];
// Construct the expected URL for the image.
NSURL *imageFileURL = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask][0];
imageFileURL = [imageFileURL URLByAppendingPathComponent:[[self.content objectAtIndex:indexPath.row] valueForKey:@"cityImage"] isDirectory:NO];
// Attempt to load the image at the above URL.
cell.imageView.image = [[UIImage alloc] initWithContentsOfFile:[imageFileURL path]];
if (!cell.imageView.image)
cell.imageView.image = [UIImage imageNamed:[[self.content objectAtIndex:indexPath.row] valueForKey:@"cityImage"]];
}
return cell;
}
再次,它沒有工作。下面是截圖:
你絕對應該做紀錄片的一些閱讀。你的措詞是完全混淆的。在'cellForRowAtIndexPath:'你不要繼承這個單元格。你使用一個分類的單元格。 – vikingosegundo