vpbot
vpbot

Reputation: 2487

Indent tableview content

I have a rather simple question about TableView's in iOS, is there a way to indent the contents of the cells in a tableview, say for example I have an array of strings, and I want to have them displayed on the ui as alternating series of text, it would look like a conversation log. eg.

I am Hercules
                     I am Delilah
If you are Delilah
             Then I am your lover

This without resorting to custom cells, however, if the custom cells is the only, can you please show me how/where to start?

TIA

mirage

Upvotes: 0

Views: 131

Answers (2)

Rawkode
Rawkode

Reputation: 22592

Within your cellForRowAtIndexPath method, do something such as this:

if (indexPath.row % 2 == 0) {
  [cell.textLabel setTextAlignment:UITextAlignmentLeft];
} else {
  [cell.textLabel setTextAlignment:UITextAlignmentRight];
}

Updated with correct alignment, thanks paul polo

Upvotes: 2

pro_metedor
pro_metedor

Reputation: 1176

You can hook up when creating a cell in tableView : (UITableView *)tableView cellForRowAtIndexPath method:

  1. Check basing on indexPath whether you are creating odd or even cell.
  2. Set text alingment for label within the cell. (you may alternate its origin point as well)

And you're done!

Upvotes: 0

Related Questions