Reputation: 31
Sorry this is such a new question, but I searched all day and still cannot find the answer. When I run the app, it shows the rows with the label and count only, but the image is not showing up on the table view cell.
#import "ViewController.h"
@interface ViewController () <UITableViewDataSource, UITableViewDelegate> {
NSMutableArray * imageNameArray;
//__weak IBOutlet UIImageView *cell;
__weak IBOutlet UITableView *Table;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self arraySetup];
}
-(void) arraySetup {
imageNameArray = [NSMutableArray arrayWithArray:@[@"2.jpg"]];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {
return imageNameArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellId = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
//cell.imageView.image = [UIImage imageNamed:imageNameArray [indexPath.row]];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
cell.textLabel.text = imageNameArray [indexPath.row];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", (int) indexPath.row + 1];
UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(10,5, 50, 25)];
imv.image=[UIImage imageNamed:@"2.jpg"];
[cell.contentView addSubview:imv];
cell.imageView.image = imv.image;
return cell;
}
Upvotes: 2
Views: 2428
Reputation: 178
I know this is very late reply and only few people are using Objective C these days .Below is working for me . Please note that I am reusing same tableView for populating address-books and when click on each address book loading it's contacts . So you can just remove if conditions.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"cells";
UITableViewCell * cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (_isPhoneBooksLoaded){
cell.imageView.image =nil;
}
if (_isContactsLoaded){
cell.imageView.image = [UIImage imageNamed:@"contact_default.png"];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
Important :
Do not forget to use same cell identifier you set .
Remove if conditions isPhoneBooksLoaded,isContactsLoaded and just keep
cell.imageView.image = [UIImage imageNamed:@"contact_default.png"];
Upvotes: 0
Reputation: 336
Here is some very basic code for you to learn from:
I'm using the basic class UITableViewCell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.textLabel.text = @"Your table row text";
cell.imageView.image = [UIImage imageNamed:@"image.png"];// Here you specify your image
cell.detailTextLabel.text = @"Your descriptive text";
return cell;
}
Upvotes: 1
Reputation: 6643
You must init the imageView at the init method of cell like:
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(10,5, 50, 25)];
imv.tag = 10;
[cell.contentView addSubview:imv];
}
and the you can set the image by
cell.textLabel.text = imageNameArray [indexPath.row];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", (int) indexPath.row + 1];
UIImageView *imv = (UIImageView *)[cell.contentView viewWithTag:10];
imv.image=[UIImage imageNamed:@"2.jpg"];
return cell;
Upvotes: 0
Reputation: 340
add array in ViewDidLoad:
arrayValue = [[ "userEmailType": "Home", "userImage": "image1.png"], [ "userEmailType": "Home", "userImage": "image2.png"], [ "userEmailType": "Work", "userImage": "image3.png"],]
Custom UITableViewCell code:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: CustomCell! = tableView.dequeueReusableCell(withIdentifier: "CustomCellIdentifier", for: indexPath) as! CustomCell
// Static image
cell.checkedImageObj!.image = UIImage.init(named: "resort_selected")
//Want round Image....
cell.contactImage.layer.cornerRadius = cell.contactImage.frame.size.width/2
cell.contactImage.layer.masksToBounds = true
// Set array of Image
cell.contactImage.image = (arrayValue[indexPath.row] as! NSDictionary).value(forKey: "userImage") as? UIImage
}
Please try it.... Thanks
Upvotes: 0
Reputation: 3301
By default the UITableViewCell
has the UIImageView
. Try this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellId = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
cell.textLabel.text = imageNameArray [indexPath.row];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", (int) indexPath.row + 1];
cell.imageView.image = [UIImage imageNamed:@"2.jpg"];
return cell;
}
Another approach is to use your own Custom TableViewCell.
Upvotes: 0