Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell set style programmatically

How to create UITableViewCell with an next style (Right Detail in xib)

enter image description here

For example can I use next:

cell.style = 'Right Detail' (roughly)

Thanks!

like image 668
Matrosov Oleksandr Avatar asked Mar 28 '12 17:03

Matrosov Oleksandr


People also ask

How do I change the Tableview style in Swift?

style is a readonly property of UITableView . That means you can initialize a table as grouped or plane , but you cannot change the style afterwards. Save this answer.

What is reuse identifier in Xcode?

The reuse identifier is associated with a UITableViewCell object that the table-view's delegate creates with the intent to reuse it as the basis (for performance reasons) for multiple rows of a table view. It is assigned to the cell object in initWithFrame:reuseIdentifier: and cannot be changed thereafter.


2 Answers

What you want is UITableViewCellStyleValue1, but you can't set an existing cell's style: you have to set it when you're creating it. Like this:

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"YourIdentifier"];
like image 151
yuji Avatar answered Oct 14 '22 08:10

yuji


While the best answer for this question is correct it doesn't touch on the topic of cell reuse.

It turns out you no longer have to use tableView.register. Instead you can use this approach:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    // Whenever there's a reusable cell available, dequeue will return it
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier") ??
        UITableViewCell(style: .subtitle, reuseIdentifier: "cellIdentifier")
like image 26
csch Avatar answered Oct 14 '22 10:10

csch