Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在Swift中更改UITableView单元格文本宽度_Ios_Uitableview_Cell - Fatal编程技术网

Ios 如何在Swift中更改UITableView单元格文本宽度

Ios 如何在Swift中更改UITableView单元格文本宽度,ios,uitableview,cell,Ios,Uitableview,Cell,我试图在UITableview中更改单元格文本的宽度 这就是我看到的容器的宽度 var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell cell.textLabel?.text = users[indexPath.row].User cell.detailTextLabel?.text=String(

我试图在UITableview中更改单元格文本的宽度

这就是我看到的容器的宽度

var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
    cell.textLabel?.text = users[indexPath.row].User
    cell.detailTextLabel?.text=String(users[indexPath.row].Score)
   
    cell.textLabel?.bounds.width=20
我想展示一下(截尾):


如果您使用的是自动布局,您可以将数字固定在容器视图的右边缘,放置一个水平垫片,并为名称标签提供比数字标签更低的抗压优先级。这将使名称标签尽可能宽,但不会太宽,以致于它会夹在数字中。

我认为screen short似乎已经达到屏幕宽度。因此,增加标签宽度是没有用的。如果您想显示文本标签的全文,您可以遵循以下任一解决方案

cell.textLabel?.adjustsFontSizeToFitWidth = true;
它根据标签宽度调整字体大小

 cell.textLabel?.numberOfLines = 0
它使标签文本显示为两行

编辑

如果要截断文本标签的尾部,请尝试此操作

cell.textLabel?.lineBreakMode = NSLineBreakMode.ByTruncatingTail

实际上,在TableView中不允许更改单元格中textLabel的框架。每个单元格的textlabel的边框宽度与该单元格相关,开发人员无法通过编程进行设置。然而,我找到了一个可行的解决方案:我们可能不会更改textLabel的框架,但我们可以交替更改源文本。如果我们可以提前截断源字符串,这个问题就可以解决

//Assign each element in the array a row in table view

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    var myCell = myTableView.dequeueReusableCell(withIdentifier: "The Cell")

    if myCell == nil {
        myCell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: "The Cell")
    }

    myCell?.accessoryType = UITableViewCellAccessoryType.none

    // If the text is too long (longer than 20 char), the text will be truncated
    // The lenght of label cannot be set effectively here, so I truncate the source string alternatively
    var myString: String = sectionArray[indexPath.section].items[indexPath.row]
    if myString.characters.count > 20 {

        let myIndex = myString.index(myString.startIndex, offsetBy: 20)
        myString = myString.substring(to: myIndex)
        myString += "..."
    }

    myCell?.textLabel?.text = myString
    myCell?.detailTextLabel?.text = "\(NSDate())"

    return myCell!
}

使用此方法,可以实现更改textLabel框架的效果。

感谢您的回复,我希望通过提供最大宽度来包装文本。如果您希望截断尾部,请使用此代码。cell.textLabel?.lineBreakMode=NSLineBreakMode.ByTruncingTail不起作用,您得到的输出是否与上面的屏幕短?是的,相同的输出没有任何变化