0

I'm working on a Xamarin.Forms project at the moment, which requires a custom layout for a cell.

I've written the WP and Droid layouts, but stuck on iOS. The layout is pretty simple:

  • Two columns, two rows. Rows are same height, first column is 96px wide and rest is taken by the second
  • The first column is filled in by a number, spanning both rows
  • The first row of the second column is filled by a title
  • The second row of the second column is filled by a detail text, aligned left, and a date, aligned right.
  • If the detail text is too long, it is trunkated, date is always displayed

Now for this, I wrote a custom renderer, which uses native elements (XamForms' ViewCell is extremely slow, 6x slower than e.g. TextCell or ImageCell). But on iOS, the only platform I've never really worked with, I ran into a problem.

I've created a class, inheriting from UITableViewCell, and added 4 labels to it: titleLabel, detailLabel, dateLabel, and bigNumLabel (latter being the first column's content).

I fill these labels, and add them to the Subviews, but when it gets to actually assigning them to an area, I'm pretty much shot. I'm using the following code:

public override void LayoutSubviews() { base.LayoutSubviews(); bigNumLabel.Frame = new CoreGraphics.CGRect(0, 0, 96, 96); titleLabel.Frame = new CoreGraphics.CGRect(96, 12, 300, 38); detailLabel.Frame = new CoreGraphics.CGRect(96, 62, 300, 38); dateLabel.Frame = new CoreGraphics.CGRect(396, 62, 100, 38); }

Now, the only issue I have is, I'm used to WP's (and XamForms') uber-simple relative layouts. On WP, I could make a grid, two columns, two rows, and align the data relatively - bigNumLabel in row0 column0, with rowspan2, titleLabel in row0 column1, aligned left, detailLabel in row1 column1, aligned left, and dateLabel in row1 column1 aligned right. On iOS, however, I have to specify down to a pixel level - how is it easier than WP then? Why do people perfer this old system instead of a truly flexible layout?

But that is not my question at the moment. Rather, I'm asking, how to align these parts so that they don't overlap, and are not fixed at a given pixel - but rather as a flow?

1 Answer 1

0

I would suggest using Auto Layout to keep your label align and not overlapped. Since you may be already using native view for the UITableViewCell, you may consider using the designer for easier control over the constraints in Auto Layout.

1
  • As I'm using Xamarin.Forms, I'm not sure if I can use a designer-made element - all samples so far have been code-behind only, and my little to no experience in iOS development does not help either.
    – fonix232
    Mar 4, 2015 at 21:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.