Reputation: 27123
How to create UITableViewCell with an next style (Right Detail in xib)
For example can I use next:
cell.style = 'Right Detail' (roughly)
Thanks!
Upvotes: 10
Views: 24620
Reputation: 1
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "TopCell", for: indexPath)
cell = UITableViewCell(style: .value1, reuseIdentifier: "TopCell")
var content = cell.defaultContentConfiguration()
content.text = "Title"
content.secondaryText = "Detail"
cell.contentConfiguration = content
return cell
}
Upvotes: 0
Reputation: 2138
If you want to use the UITableViewCell
and style it programmatically then
Don't register it to your tableView and do this in cellForRowAt
var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
if cell == nil {
var newCell = UITableViewCell(style: .value1, reuseIdentifier: cellIdentifier)
// custom styling on textLabel, detailTextLabel, accessoryType
cell = newCell
}
Note: The deque method is without IndexPath
param.
Upvotes: 0
Reputation: 1455
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")
Upvotes: 3
Reputation: 16725
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"];
Upvotes: 25