0

Problem Statement 1: Unable to align two custom cells text simultaneously for multiline, as shown below. enter image description here

You can see here, for multi-line cell both the cell contents are not at same starting top line.

When there is no multiline content, then it is ok, as shown in below

enter image description here

You can see, how both the cell contents are aligned on same line properly.

Problem Statement 2: And there is one more problem, if cell is having large content, it won't increase it's height, instead of that it gets small font size as shown below

enter image description here Below is my coding stuff, by this way I've managed for two custom cell with multiline.

func tableView(tableViewData: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {

        let cell = tableViewData.dequeueReusableCellWithIdentifier("cell")! as! StudentCell

        if indexPath.row < self.mMeaningArray.count
        {
            cell.lblMeaning1.text = self.mMeaningArray[indexPath.row]
        }
        else
        {
            cell.lblMeaning1.text = ""
        }
        if indexPath.row < self.hMeaningArray.count
        {
            cell.lblMeaning2.text = self.hMeaningArray[indexPath.row]
        }
        else
        {
            cell.lblMeaning2.text = ""
        }

        self.allowMultipleLines(cell)

        cell.lblMeaning1.layoutIfNeeded()
        cell.lblMeaning2.layoutIfNeeded()

        return cell
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
    {

        return UITableViewAutomaticDimension
    }

    func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
    {
        return UITableViewAutomaticDimension
    }

    func allowMultipleLines(tableViewCell:UITableViewCell)
    {
        tableViewCell.textLabel?.numberOfLines = 0
        tableViewCell.textLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
        tableViewCell.textLabel?.sizeToFit()
    }

I know, it is adjusting its font size, as per contents, but I want that, it should be a fixed font-size with multi-line and simultaneously it should align text properly.

I've search on various sites, links & refer multiple questions of stack-overflow, but for this situation, didn't find any solution.

iDeveloper
  • 940
  • 1
  • 10
  • 45

1 Answers1

0

solution using AutoLayout in StoryBoard.

1) Set no of lines to 0 and text alignment to Left.

Image 1

2) Set height constraint.

Image 2

3) The height Constraint should be in Relation - Less Than or Equal

Image 3

4) Add sizetofit in inside of cellForItemAtIndexPath method

labelname.sizeToFit()

5) Thats it. Now you will get the result like this. For clarity, I set label backgroundcolor as blue.

Image 4

Muthu Sabarinathan
  • 1,198
  • 2
  • 21
  • 49