UITableView,是否有可能在同一个单元格中使用左+右alignment2个string?

说我有一个string数组我在tableView中显示以下数据:

排名名称项目

第一个字段是一致的长度(4),但是中间字段是可变的,所以我希望能够做的是以某种方式拆分单元格,使得Item是正确的。 是否有捷径可寻?

排名名称项目

所以

Sarg Bobmarley magic Capt Huf axe Peon Yumkru sword 

 Sarg Bobmarley magic Capt Huf axe Peon Yumkru sword 

细胞法:

 // 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... TableViewPracticeAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; cell.textLabel.text = [[appDelegate theArray] objectAtIndex:indexPath.row]; cell.textLabel.font = [UIFont boldSystemFontOfSize:15]; } 

任何想法或想法appricated!

你必须做一个自定义的单元格。 iOS中没有NSAttributedString(更新:iOS6带来NSAttributedString)。 它的确很简单,下面是一个例子:

 // 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... } 

对于其他谁将在稍后绊倒,并尝试使用上面的答案…先试试这个:UITableViewCellStyleValue1

这是一个iOS预定义的表格单元格样式,主标签为黑色(左alignment),次文本为右蓝色(右alignment)。它们甚至可以是不同的字体。 例如。:

 -(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; } 

这几乎是… 🙂

UITableViewCell也有一个detailTextLabel属性,我相信,默认情况下是正确的。 否则,您可以使用代码或使用Interface Builder创build自定义单元格,该单元格将具有两个标签,其中一个标签具有右alignment的文本。