Table cell badge issue with CustomCells

With the help of iOSKit I was able to convert some code found on GitHub to Xojo code:

Code:

[code] //Source: http://stackoverflow.com/questions/26129428/badge-view-in-uitableviewcell

	// Count > 0, show count
	If (forCount > 0) Then
			
			// Create label
			Dim fontSize As Single = 14
			Dim lbl As New iOSLabel
			Dim Font As iOSFont = iOSFont.SystemFont(fontSize)
			lbl.TextFont = Font
			lbl.TextAlignment = iOSTextAlignment.Center
			lbl.TextColor = &cFFFFFF
			Dim p As New iOSBitmap(1, 1, 2)
			Dim g As iOSGraphics = p.graphics
			g.TextFont = Font
			
			Declare Sub setBackgroundColor Lib UIKitLib selector "setBackgroundColor:" (obj_id As ptr, col As ptr)
			setBackgroundColor(lbl.Handle, New UIColor(&cFF0000))
			
			// Add count to label and size to fit
			lbl.Text = forCount.ToText
			Declare Sub setAdjustsFontSizeToFitWidth Lib UIKitLib selector "setAdjustsFontSizeToFitWidth:" (id As ptr, value As Boolean)
			setAdjustsFontSizeToFitWidth lbl.Handle, True
			
			// Adjust frame to be square for single digits or elliptical for numbers > 9
			#If Target32Bit
					Declare Sub drawInRect Lib UIKitLib selector "drawInRect:" (obj_id As ptr, rect As CGRect32)
					Dim frame As CGRect32
			#ElseIf Target64Bit
					Declare Sub drawInRect Lib UIKitLib selector "drawInRect:" (obj_id As ptr, rect As CGRect64)
					Dim frame As CGRect64
			#EndIf
			
			#If Target32Bit
					Declare Function frame_ Lib UIKitLib selector "frame" (view As ptr) As CGRect32
			#ElseIf Target64Bit
					Declare Function frame_ Lib UIKitLib selector "frame" (view As ptr) As CGRect64
			#EndIf
			
			frame = frame_(lbl.handle)
			frame.Origin.x = 0
			frame.origin.y = 0
			frame.rsize.h = xojo.math.Round(Font.XHeight + Font.CapHeight*2)
			frame.rsize.w = If(forCount <= 9, frame.rsize.h, g.TextLineSize(lbl.Text).Width + fontSize)
			
			#If Target32Bit
					Declare Sub setFrame Lib UIKitLib selector "setFrame:" (obj_id As ptr, frame As CGRect32)
					setFrame(lbl.Handle, frame)
			#ElseIf Target64Bit
					Declare Sub setFrame Lib UIKitLib selector "setFrame:" (obj_id As ptr, frame As CGRect64)
					setFrame(lbl.Handle, frame)
			#EndIf
			
			
			// Set radius and clip to bounds
			Declare Function layer_ Lib UIKitLib selector "layer" (obj_id As ptr) As ptr
			Declare Sub clipsToBounds Lib UIKitLib selector "setClipsToBounds:" (obj_id As ptr, value As Boolean)
			#If Target32Bit
					Declare Sub setCornerRadius_ Lib "QuartzCore.framework" selector "setCornerRadius:" (obj_id As ptr, value As Single)
			#ElseIf Target64Bit
					Declare Sub setCornerRadius_ Lib "QuartzCore.framework" selector "setCornerRadius:" (obj_id As ptr, value As Double)
			#EndIf
			Dim layer As ptr = layer_(lbl.Handle)
			setCornerRadius_(layer, frame.rsize.h/2.0)
			clipsToBounds(lbl.Handle, True)
			
			
			//Show label in accessory view and remove disclosure
			Declare Sub setAccessoryView Lib "UIKit" selector "setAccessoryView:" (obj_id As ptr, acc_id As ptr)
			setAccessoryView(cell.Handle, lbl.Handle)
			
			cell.AccessoryType = iOSTableCellData.AccessoryTypes.None
			
			
	Else
			cell.AccessoryType = iOSTableCellData.AccessoryTypes.Disclosure
	End If[/code]

This code works perfectly as long as the Table has no CustomCells.

In the screenshot below, “Semester 1 | Semester 2” is a CustomCell. The cell badge for the Exceptions row is placed at 0:0 in the cell. As if the AccessoryView was smaller and placed at the top left corner.

Why don’t you draw in a canvas? It’s much simpler

Create in the custom cell a canvas on the right side of the cell and your Label with a constrain vs. the canvas (LabelRight< gap > CanvasLeft) and give a name to this constrain

When you assign the value to the “badge” in the cell:
If it is 0 set this constrain to 0 (the Label will expand if necessary) but since you set the accessory the cell will be smaller;
if it is >0 calculate the text width for number (plus round space) and set this value as the constrain’s offset.
memorize the value and call canvas.invalidate

More complicated to describe than to code it

Much simpler… yes and no.

I can handle setting up a canvas to display the badge. But I would also need to:

  • Handle an imageView to display a cell icon
  • Handle the alignment of labels if imageView is empty
  • Handle two labels for text and detailText
  • Handle the label positions if I decide to only display text, or only display detailText
  • Find some pictures to display a checkmark and a disclosure icon when the badge value is 0

And if next iOS major release changes the alignment of some element in TableView cells, I’ll need to update the App. I’d rather stay with a native control for something as simple as a cell badge.

You can still access the standard cell in a custom cell.
So you can only use the custom cell to draw the badge

I didn’t know that the standard cell was still accessible in a Custom Cell. Grazie mille Antonio