systemLayoutSizeFittingSize not sizing UITableViewCell correctly - swift

I have a UITableViewCell with a label inside of it. I would like to calculate the cell size based on the contents inside of it.
I don't just set constraints in the content view though, I also add constraints to the enclosing UITableViewCell:
+--------------------------+
|UITableViewCell |
| | inset |
| +------------------+ |
| |contentView | |
| | |inset | |
| | +------------+ | |
|-- |--| Label |--| --|
| | +------------+ | |
| | |inset | |
| +------------------+ |
| |inset |
+--------------------------+
And here is the code that calculates the size:
override public class func cellSize(item: ItemInterface?, fittingSize: CGSize) -> CGSize {
struct Static {
static var onceToken : dispatch_once_t = 0
static var sizingCell : LabelTableViewCell!
}
dispatch_once(&Static.onceToken, {
Static.sizingCell = NSBundle.mainBundle().loadNibNamed("LabelTableViewCell", owner: self, options: nil)[0] as! LabelTableViewCell
})
let sizingCell = Static.sizingCell
// sets the text of the label and also adds constraints
// from label to enclosing content view
sizingCell.setupCell(text: "asdkfjklsd")
// for multi line support
sizingCell.label.preferredMaxLayoutWidth = fittingSize.width
// update all the constraints
sizingCell.setNeedsUpdateConstraints()
sizingCell.updateConstraintsIfNeeded()
// re-layout cell
sizingCell.setNeedsLayout()
sizingCell.layoutIfNeeded()
// calculate size for the whole cell (not just contentView)
let size = sizingCell.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
return CGSizeMake(size.width, size.height)
}
What I end up getting is a cell that is squished. The label ends up being too small and therefore you almost don't see the label at all:

Related

How to rename column of a DataFrame from a list

I have a dataframe like this:
dog
cat
Cell 1
Cell 2
Cell 3
Cell 4
And a list like this:
dog, bulldog
cat, persian
I would like to create a function that find the name of the column in the list and substitute it with the second element (bulldog, persian).
So the final result should be:
| bulldog | persian |
| -------- | -------- |
| Cell 1 | Cell 2 |
| Cell 3 | Cell 4 |
You need to perform a look-up for your original column in the pre-defined list that you have shown. It's easier to create a Map out of it so lookups can be performed:
val list: List[(String, String)] = List(("dog", "bulldog"), ("cat", "persian"))
val columnMap = list.toMap
// columnMap: scala.collection.immutable.Map[String,String] = Map(dog -> bulldog, cat -> persian)
val originalCols = df.columns
​
val renamedCols = originalCols.map{
c => if (columnMap.keys.toArray.contains(c)) s"${c} as ${columnMap.getOrElse(c, "")}"
else c
}
println(renamedCols)
// renamedCols: Array[String] = Array(dog as bulldog, cat as persian)
df.selectExpr(renamedCols: _*).show(false)
// +-------+-------+
// |bulldog|persian|
// +-------+-------+
// |Cell 1 |Cell 2 |
// |Cell 1 |Cell 2 |
// +-------+-------+

Using Perfect-Markdown, tables are not rendering like GitHub's UI

I'm using the Perfect framework and importing Perfect-Markdown to render my README.md on the landing page of my REST service.
Simply put, I'm attempting to render a table that looks something like:
| Tables | Are | Cool |
| ------------- |:-------------:| -----:|
| col 3 is | right-aligned | $1600 |
| col 2 is | centered | $12 |
| zebra stripes | are neat | $1 |
My handler is:
func handler(request: HTTPRequest, response: HTTPResponse) {
response.setHeader(.contentType, value: "text/html")
let source = getSourceDir(for: #file)
let data = FileManager.init().contents(atPath: "\(source)README2.md")
let string = String.init(data: data!, encoding: .utf8)
let html = string?.markdownToHTML
response.appendBody(string: html!)
response.completed()
}
getSourceDir() is simply applying the path of the file in the project.
I really want to the table to display like it does with GitHub's UI, but instead the table displays like:
Tables Are Cool
col 3 is right-aligned $1600
col 2 is centered $12
zebra stripes are neat $1
There is no "table" to speak of...

How to define / change MLDataValue.ValueType for a column in MLDataTable

I am loading a MLDataTable from a given .csv file. The data type for each column is inferred automatically depending on the content of the input file.
I need predictable, explicit types when I process the table later.
How can I enforce a certain type when loading a file or alternatively change the type in a second step?
Simplified Example:
import Foundation
import CreateML
// file.csv:
//
// value1,value2
// 1.5,1
let table = try MLDataTable(contentsOf:URL(fileURLWithPath:"/path/to/file.csv"))
print(table.columnTypes)
// actual output:
// ["value2": Int, "value1": Double] <--- type for value2 is 'Int'
//
// wanted output:
// ["value2": Double, "value1": Double] <--- how can I make it 'Double'?
Use MLDataColumn's map(to:) method to derive a new column from the existing one with the desired underlying type:
let squaresArrayInt = (1...5).map{$0 * $0}
var table = try! MLDataTable(dictionary: ["Ints" : squaresArrayInt])
print(table)
let squaresColumnDouble = table["Ints"].map(to: Double.self)
table.addColumn(squaresColumnDouble, named: "Doubles")
print(table)
Produces the following output:
Columns:
Ints integer
Rows: 5
Data:
+----------------+
| Ints |
+----------------+
| 1 |
| 4 |
| 9 |
| 16 |
| 25 |
+----------------+
[5 rows x 1 columns]
Columns:
Ints integer
Doubles float
Rows: 5
Data:
+----------------+----------------+
| Ints | Doubles |
+----------------+----------------+
| 1 | 1 |
| 4 | 4 |
| 9 | 9 |
| 16 | 16 |
| 25 | 25 |
+----------------+----------------+
[5 rows x 2 columns]

Scala placeholder _ variable

I am learning from this site
http://naildrivin5.com/scalatour/wiki_pages/ExplcitlyTypedSelfReferences/
trait BaseComponent {
protected var label:Label = _
}
In this case what does the placeholder _ stands for?What would be the alternative?
The placeholder syntax for a variable assings the default value to the variable. Assuming Label inherits AnyRef, that would be null.
The Scala language specification lays this out explicitly:
4.2 Variable Declarations and Definitions:
A variable definition var x: T = _ can appear only as a member of a template. It introduces a mutable field with type T and a default initial value. The default value depends on the type T as follows:
| default | type T |
|---------|----------------------------------|
| 0 | Int or one of its subrange types |
| 0L | Long |
| 0.0f | Float |
| 0.0d | Double |
| false | Boolean |
| () | Unit |
| null | all other types |

Setting a custom design size for a UITabViewController (in Storyboard)

I want to modify an app that currently has a UITabBarController as its initial view controller.
The goal is to have a custom status bar in the top area of the screen that will always be shown no matter which tab is selected. The current UITabBarController may not use the full height of the screen:
/----------------------------\
|Custom Status bar (50 px) |
| |
|----------------------------|
| |
|----------------------------| ---
| | |
| | |
|View of the selected tab | |
| | |
| | |
| | |
| | |
| | smaller height of the UITabBarController
| | |
|----------------------------| |
|Tab bar | |
| | |
\----------------------------/ ---
I use storyboards. I cannot set a (design) size in the Size Inspector window even with simulated metrics size set to 'freeform'.
You can create your UITabBar programmatically like so:
UITabBar *myTabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
[MyView addSubview:myTabBar];
You should see 's documentation on the subject for more information about setting up your UITabBar programmatically.
Unfortunately, this can not be done in a storyboard or xib.
The only way to put a UITabBarController inside of another view controller is by creating a container view controller and adding it to that in code.
In this case, you would need to create a new UIViewController, and then call addChildViewController:childController for each of the view controllers that you want to display (once for your header, and once for the tab bar controller).
See the Implementing a Container View Controller section of the UIViewController class reference for more information.