Goober
Goober

Reputation: 13508

C# Monotouch/Xamarin - UITableViewCell GetCell ImageView Frame Not Resizing

Summary Overview

I have the following override GetCell in my UITableViewSource. Everything works fine, but I want to make the image on the left-hand side of the cell smaller, without having to resize it. Essentially I just want to reduce the frame to say, (30 width x 30 height)....and perhaps round the corners off (similar to Whatsapp's list of open chats).

Problem

Simply applying a new RectangleF to the Frame property of the ImageView doesn't seem to work.

 if (tableItems[indexPath.Section].Items[indexPath.Row].ImageName != null)
                                {
//RESIZE IMAGE VIEW
cell.ImageView.Frame = new RectangleF(5,5,30,30);
                                        Console.WriteLine("Image name: " + tableItems[indexPath.Section].Items[indexPath.Row].ImageName);
                                        cell.ImageView.Image = UIImage.FromFile (tableItems[indexPath.Section].Items[indexPath.Row].Image); 
                                }

CODE

 public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
            {
                    UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);
                    Custom_UITableItem item = tableItems[indexPath.Section].Items[indexPath.Row];
                    var cellStyle = UITableViewCellStyle.Subtitle;

                    // if there are no cells to reuse, create a new one
                    if (cell == null) 
                    {
                            cell = new UITableViewCell (cellStyle, cellIdentifier);
                    }

                    try
                    {
                            if(cellAccessoryButtons.Count > 0)
                            {
                                    var cellAccessoryButton = cellAccessoryButtons [indexPath.Row];
                                    cell.AccessoryView = cellAccessoryButton;
                            }

                            cell.TextLabel.Text = tableItems[indexPath.Section].Items[indexPath.Row].Heading;
                            cell.TextLabel.Font = AppSettings.CellDetailTextLabelFontSize;
                            cell.TextLabel.LineBreakMode = UILineBreakMode.WordWrap;
                            cell.TextLabel.Lines = 2;
                            cell.DetailTextLabel.Text = tableItems[indexPath.Section].Items[indexPath.Row].SubHeading;
                            cell.DetailTextLabel.Font = AppSettings.CellDetailTextLabelFontSize;
                            cell.DetailTextLabel.LineBreakMode = UILineBreakMode.WordWrap;
                            cell.DetailTextLabel.Lines = 2;

                            if (tableItems[indexPath.Section].Items[indexPath.Row].ImageName != null)
                            {
//RESIZE IMAGE VIEW
cell.ImageView.Frame = new RectangleF(5,5,30,30);

                                    Console.WriteLine("Image name: " + tableItems[indexPath.Section].Items[indexPath.Row].ImageName);
                                    cell.ImageView.Image = UIImage.FromFile (tableItems[indexPath.Section].Items[indexPath.Row].Image); 
                            }

                            var trackUploadedDateFrame = new RectangleF (87, 55, 100, 20);
                            var trackUploadedDate = new UILabel (trackUploadedDateFrame);
                            trackUploadedDate.BackgroundColor = UIColor.Clear;
                            trackUploadedDate.Font = AppSettings.TrackUploadedDateFontSize;
                            trackUploadedDate.TextAlignment = UITextAlignment.Left;
                            trackUploadedDate.Text = "Uploaded: 16/10/13";
                            cell.AddSubview (trackUploadedDate);

                    }
                    catch(Exception ex) 
                    {
                            Console.WriteLine (ex.ToString ());
                    }

                    return cell;
            } 

Upvotes: 0

Views: 1521

Answers (2)

Sharmila Sonavane
Sharmila Sonavane

Reputation: 51

Use the following function to resize an image and then set it into table cell image view

 public UIImage ResizeImage(UIImage sourceImage, float width, float height)
        {
            UIGraphics.BeginImageContext(new SizeF(width, height));
            sourceImage.Draw(new RectangleF(0, 0, width, height));
            var resultImage = UIGraphics.GetImageFromCurrentImageContext();
            UIGraphics.EndImageContext();
            return resultImage;
        }

Upvotes: 0

Oliver Weichhold
Oliver Weichhold

Reputation: 10296

In short "been there, done that" and let me tell you customizing the stock UITableViewCell - especially it's ImageView is a royal pain.

From my experience its easier to achieve your design goal by subclassing UITableViewCell - either in Code or .xib using IB - unless the functionality of the stock UITableViewCell is very close to your own goals.

Upvotes: 1

Related Questions