jameshwart lopez
jameshwart lopez

Reputation: 3151

how to add image in a static cell of a table view programmatically

I have added an image view in one of the image view.I could have use Attributes inspector and then change the image property but i want to do it programmatically.

enter image description here

So far this is what i have done and get the following error

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier account_cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cell:UITableViewCell! = UITableViewCell()


        if indexPath.section == 0{

            if indexPath.row == 0{
                cell = tableView.dequeueReusableCellWithIdentifier("account_cell", forIndexPath: indexPath)

                if let AccountImage = cell.viewWithTag(100) as? UIImageView{
                     AccountImage.image = UIImage(named: "Friend_g")?.imageWithRenderingMode(.AlwaysOriginal)
                }



            }
        }
        print("\(indexPath)")
        return cell
    }

Can someone give me a beginners guide how can i achieve it programmatically.Any help would be appreciated.Thanks in advance.

Upvotes: 1

Views: 2292

Answers (2)

Dharmesh Kheni
Dharmesh Kheni

Reputation: 71854

You don't need to use cellForRowAtIndexPath for assign image in your cell because you cells are statics.

One easy way to assign image into static cell is just create IBOutlet of that imageView this way:

@IBOutlet weak var UserImage: UIImageView!

After that assign image to UserImage in your viewDidLoad method this way:

override func viewDidLoad() {

    UserImage.image = UIImage(named: "Friends_g")
    self.tableView.separatorColor = UIColor.clearColor()
}

And result will be:

enter image description here

Upvotes: 2

Tejas Patel
Tejas Patel

Reputation: 870

I am working with Objective-c and in Objective-c i can add image in cell using this following code.

UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
imv.image=[UIImage imageNamed:@"user.jpg"];
[cell.contentView addSubview:imv];

Upvotes: 0

Related Questions