标签不适合TableView

时间:2017-04-05 05:00:47

标签: ios objective-c uitableview

我试图在labels上添加tableView(左侧和右侧)。但是,label texts被削减,我想知道如何解决。我正在添加我的源代码以及附加屏幕捕获。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

    cell.imageView.image = [UIImage imageNamed:@"placeholder.png"];
    cell.textLabel.text = [[dataArray objectAtIndex:indexPath.row] objectForKey:@"name"];
    cell.detailTextLabel.text= [NSString  stringWithFormat:@"$ %@", [[dataArray objectAtIndex:indexPath.row] objectForKey:@"price"]];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
         return 70.0;
}

enter image description here

4 个答案:

答案 0 :(得分:2)

你可以用两种方式做到这一点

您需要将文本对齐更改为右侧,例如

cell.textLabel.textAlignment = NSTextAlignmentRight;
cell.detailTextLabel.textAlignment = NSTextAlignmentRight;

或在Right Detail

中的属性检查器中将您的UITableViewCell样式更改为XCode

enter image description here

将您的UITableViewCellStyleDefault更改为UITableViewCellStyleValue1UITableViewCellStyleValue2

您可以在UITableViewCell class

中获取样式属性

enter image description here

并调用

   if (cell == nil)
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellIdentifier];
    cell.imageView.image = [UIImage imageNamed:@"placeholder.png"];
    cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
    cell.detailTextLabel.text= @"Yesterday";

有关详细信息,您可以获取示例here否则创建自定义单元格并根据您的需要将标签设置为右对齐

答案 1 :(得分:0)

更改以下代码行:

   cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];

试试这个:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
   cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];

    cell.imageView.image = [UIImage imageNamed:@"placeholder.png"];
    cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
    cell.detailTextLabel.text= @"Yesterday";

    return cell;
}

希望它有所帮助!

答案 2 :(得分:0)

应该是这样的

- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellIdentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }

    cell.imageView.image = [UIImage imageNamed:@"placeholder.png"];
    cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
    cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
    cell.textLabel.textAlignment = NSTextAlignmentLeft;
    cell.textLabel.font=[UIFont fontWithName:@"Arial" size:12];
    cell.textLabel.adjustsFontSizeToFitWidth = YES; 
    cell.detailTextLabel.text= @"Yesterday";
    cell.detailTextLabel.textAlignment = NSTextAlignmentRight;
    cell.detailTextLabel.font=[UIFont fontWithName:@"Arial" size:12];//If you want decrease font size here
    cell.detailTextLabel.adjustsFontSizeToFitWidth = YES; 

    return cell;
}

答案 3 :(得分:0)

  

您可以使用NSLineBreakByCharWrapping

包装标签

在此处查看此代码myLabelWidth是您的标签宽度,myLabelHeight是您的标签高度

        CGSize size = [textLabel.text sizeWithAttributes:
                   @{NSFontAttributeName:
                         [UIFont systemFontOfSize:17.0f]}];
    int sizeofL=size.width;
    if(sizeofL>(mylabelWidth))
    {
        [textLabel setMinimumFontSize:5.0];
        [textLabel setNumberOfLines:0];

        int hgtofL=size.height;
        if(hgtofL>mylabelHieght)
        {
            if(height<1024)
                textLabel.font = [UIFont systemFontOfSize:12];
            else
                textLabel.font = [UIFont systemFontOfSize:14];
        }
        else
        {
            if(height<1024)
                textLabel.font = [UIFont systemFontOfSize:16];
            else
                textLabel.font = [UIFont systemFontOfSize:18];
        }

        textLabel.lineBreakMode = NSLineBreakByCharWrapping;
        [textLabel sizeToFit];
    }
    else
    {
        if(height<1024)
            textLabel.font = [UIFont systemFontOfSize:16];
        else
            textLabel.font = [UIFont systemFontOfSize:18];
    }

    cell.separatorInset = UIEdgeInsetsMake(0, 10000, 0, 0);
    [cell.contentView addSubview:textLabel];
    [cell setEditing:NO];
}