UITableView, is it possible to have 2 strings in the same cell using left+right justify?
Say I have an array of strings I am displaying the following data in a tableView:
Rank Name Item
The first field is consistant length (4), however the middle field is variable so what I would love to be able to do is somehow split the cell so that Item is right justified. Is there an easy way to do this?
Rank Name Item
So
Sarg Bobmarley magic
Capt Huf axe
Peon Yumkru sword
Becomes
Sarg Bobmarley magic
Capt Huf axe
Peon Yumkru sword
Cell method:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
TableViewP开发者_运维问答racticeAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
cell.textLabel.text = [[appDelegate theArray] objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
}
Any thoughts or ideas appricated!
You have to make a custom cell. There's no NSAttributedString in iOS (Update: iOS6 brings NSAttributedString). Its simple really, here's an example:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
UILabel *rank = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 100, 20];
//Mess around with the rects, I am just merely guessing.
[rank setTag:5];
[[self contentView] addSubView:rank];
[rank release];
UILabel *item = [[UILabel alloc] initWithFrame:CGRectMake(110, 5, 220, 20];
//Important
[item setTextAlignment:UITextAlignmentRight];
[item setTag:6];
[[self contentView] addSubView:item];
[item release];
}
UILabel *rank = (UILabel *)[cell viewWithTag:5];
UILabel *item = (UILabel *)[cell viewWithTag:6];
//And now set their texts...
}
For the others who are going to stumble on this later and try to use the above answer... Try this first: UITableViewCellStyleValue1
This is an iOS pre-defined table cell style with Main label in black (left justified) and secondary text on right in blue (right justified) They can even be different fonts. Eg.:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if( cell == nil ) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell1"] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.font = [UIFont fontWithName:@"myFont" size:30];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
}
cell.accessoryView = [Some widget you want on the right... or not];
cell.textLabel.text = @"Some Left justified string in whatever font here (BLACK)";
cell.detailTextLabel.text = @"Some right justified string here... in whatever font you want (BLUE)";
return cell;
}
That is pretty much it... :)
UITableViewCell
also has a detailTextLabel
property that, I believe, by default is right justified. Otherwise, you could create a custom cell either in code or using Interface Builder that would have two labels, one of which has right-justified text.
精彩评论