UITableView,是否可能有两个字符串在同一单元格使用左+右对齐? [英] UITableView, is it possible to have 2 strings in the same cell using left+right justify?

查看:85
本文介绍了UITableView,是否可能有两个字符串在同一单元格使用左+右对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Say I have an array of strings I am displaying the following data in a tableView:

排名名称

第一个字段是连续长度(4),但是中间字段是可变的,所以我想要能够做的是以某种方式拆分单元格,使项目右对齐。是否有简单的方法来做到这一点?

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?

排名名称

所以

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

任何想法或想法都可以评价!

Any thoughts or ideas appricated!

推荐答案

您必须创建自定义单元格。在iOS中没有NSAttributedString(更新:iOS6带来了NSAttributedString)。它的简单真的,这里有一个例子:

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

}

这篇关于UITableView,是否可能有两个字符串在同一单元格使用左+右对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆