Celda personalizada de código puro reimpreso-rápido (interfaz de chat qq)

Enlace original: https://blog.csdn.net/xingyun1992/article/details/51172048

Introducción

También soy un principiante, corrígeme si cometes algún error ~

La mayoría de los ejemplos en línea son ejemplos de lenguaje oc, rápido es relativamente raro, escribiré un

Primero suelta la imagen del efecto
Imagen de efecto

Se siente cien veces más problemático que Android, la altura del texto debe ser calculada por usted mismo y la altura de la fila de la celda debe ser calculada por usted mismo. (Se puede resolver un wrap_content en Android)
¿Por qué no personalizar xib? Dije que la situación es más complicada, no sé cómo calcular la altura y hay problemas con el estiramiento de la imagen.

Iniciar texto

1. Importe imágenes, archivos plist, diccionario al modelo

import UIKit

class Msg: NSObject {
    
    

    var text :String!
    var time :String!
    var type :NSNumber!

    override init() {
    
    
        super.init()
    }

    init(dir : Dictionary<String,AnyObject>) {
    
    
        super.init()
        setValuesForKeysWithDictionary(dir)
    }

    func getMsgArray() -> Array<Msg> {
    
    
        var array : Array<Msg> = []
        //取到plist文件路径
        let diaryList:String = NSBundle.mainBundle().pathForResource("messages", ofType:"plist")!
        //取出plist文件数据
        let arraylist = NSMutableArray(contentsOfFile: diaryList)!

        for i in arraylist {
    
    
            let msg = Msg(dir: i as! Dictionary<String, AnyObject> )
            array.append(msg)

        }

        return array

    }

}

He estado trabajando en él durante mucho tiempo. No hay Array en oc, solo NSArray. La diferencia entre los dos es muy grande. Array es muy similar a ArrayList en Java. Necesita ser genérico y es una estructura. El método clave setValuesForKeysWithDictionary, el tipo de variable y el nombre de la clase de modelo deben ser coherentes con el archivo plist.

2. Cree una celda personalizada para heredar UITableViewCell

La siguiente es una creación paso a paso de una celda personalizada de código puro

2.1 Declarar variables e inicializar

import UIKit

class MsgCodeCell: UITableViewCell {
    
    
    var textmsg :UIButton?
    var icon :UIImageView?
    var time :UILabel?
    var screenWdith : CGFloat?

    override func awakeFromNib() {
    
    
        super.awakeFromNib()
        // Initialization code 

        //xib自定义cell
    }

    required init?(coder aDecoder: NSCoder) {
    
    
        super.init(coder: aDecoder)
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    
    
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //代码自定义cell的入口方法~
        textmsg = UIButton();
        icon = UIImageView();
        time = UILabel();
        
        //这里做一些基本属性设置
        time?.bounds = CGRect(x: 0, y: 0, width: 80, height: 10)
        time?.font = UIFont.boldSystemFontOfSize(12)

        screenWdith = UIScreen.mainScreen().bounds.size.width

        //先不设置frame和content
        contentView.addSubview(textmsg!)
        contentView.addSubview(icon!)
        contentView.addSubview(time!)
    }

Después de la inicialización, establezca algunas propiedades básicas, como fuentes, y ciertas posiciones y tamaños que se pueden determinar. Las posiciones específicas solo se pueden determinar cuando se asignan datos externos.

3. Proporcionar un método externo para configurar el contenido de la celda y la posición de control interno.

func setMsgModle(msg:Msg) {
    
    
        //1.设置时间
        time?.text = msg.time
        time?.center = CGPoint(x: contentView.center.x, y: 10)

        //2.设置头像
        var textX : CGFloat = 0
        var iconX : CGFloat = 0
        var backimage : UIImage!

        //计算文字宽高!
        let size = NSString(string: msg.text).boundingRectWithSize(CGSizeMake(screenWdith!-140, CGFloat(MAXFLOAT)), options: NSStringDrawingOptions.UsesLineFragmentOrigin , attributes: [NSFontAttributeName:UIFont.boldSystemFontOfSize(14)], context: nil).size

        if msg.type == 0 {
    
    
            //发送者
            iconX = screenWdith! - 60
            icon?.image = UIImage(named: "me")
            textX = screenWdith! - size.width-100
            backimage = UIImage(named: "chat_send_nor")
        }else{
    
    
            //接收者
            iconX = 10
            icon?.image = UIImage(named: "other")
            backimage = UIImage(named: "chat_recive_press_pic")
            textX = 70
        }

        icon?.frame = CGRect(x: iconX, y: 30, width: 50, height: 50)

        //3.设置正文,设置button显示
        textmsg?.setTitle(msg.text, forState: UIControlState.Normal)
        textmsg?.frame = CGRect(x: textX, y: 30, width: size.width+30, height: size.height+30)
        textmsg?.titleLabel?.font = UIFont.boldSystemFontOfSize(14)
        textmsg?.titleLabel?.numberOfLines=0
        textmsg?.contentEdgeInsets = UIEdgeInsetsMake(15,15, 15, 15);

        let inset = UIEdgeInsets(top: (backimage?.size.height)!*0.5 , left: (backimage?.size.width)!*0.5, bottom: (backimage?.size.height)!*0.5, right: (backimage?.size.width)!*0.5)

        let newimage =  backimage?.resizableImageWithCapInsets(inset)

        textmsg?.setBackgroundImage(newimage, forState: UIControlState.Normal)
    }

La clave aquí es el cálculo del ancho y alto del texto, y el cálculo del estiramiento de la imagen de fondo del texto. (Es mejor no usar la etiqueta para mostrar el texto aquí, porque será más problemático establecer el fondo imagen para la etiqueta, así que aquí se hace con el botón,)

Clave:

 let size = NSString(string: msg.text).boundingRectWithSize(
 CGSizeMake(screenWdith!-140, CGFloat(MAXFLOAT)), 
 options: NSStringDrawingOptions.UsesLineFragmentOrigin ,
 attributes: [NSFontAttributeName:UIFont.boldSystemFontOfSize(14)], 
 context: nil
 ).size

En primer lugar, el objeto String no tiene este método. Primero, conviértalo a NSString y llama a boundingRectWithSize. El primer parámetro es el ancho y alto máximo que el texto puede aceptar. El tipo es CGSize. Esto significa que el ancho máximo es el ancho de la pantalla -140 (¿Por qué restar Ir a 140? Aquí el avatar cuenta como 50, y luego el espacio es 10, más el espacio 10 + 50 + 10 + 10 + 50 + 10 en ambos lados), la altura máxima significa línea break, el segundo parámetro es el atributo de dibujo (poco claro), y el tercero es el atributo de fuente, donde puede pasar el tamaño de fuente, el tipo de fuente, etc.

let inset = UIEdgeInsets(top: (backimage?.size.height)!*0.5 , left: (backimage?.size.width)!*0.5, bottom: (backimage?.size.height)!*0.5, right: (backimage?.size.width)!*0.5)

let newimage =  backimage?.resizableImageWithCapInsets(inset)

El estiramiento de la imagen aquí significa que solo el estiramiento del medio y el entorno permanecen sin cambios ~ (el mismo significado que el de la imagen .9)

De acuerdo, la celda personalizada básicamente está terminada aquí

3. Calcule la altura de la fila

Es decir, el valor máximo de la altura del texto y la altura del avatar
Escriba el método proxy en la vista de tabla

 override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    
    
        var rowheight :CGFloat = 0
        let msg = array[indexPath.row]

        //计算文字宽高!
        let size = NSString(string: msg.text).boundingRectWithSize(CGSizeMake(screenWdith!-140, CGFloat(MAXFLOAT)), options: NSStringDrawingOptions.UsesLineFragmentOrigin , attributes: [NSFontAttributeName:UIFont.boldSystemFontOfSize(14)], context: nil).size

        //4.计算总高!
        if size.height > 20{
    
    
            // 字的高度
            rowheight = size.height+70
        }else{
    
    
            //图片的高度
            rowheight = 90
        }
        return rowheight
    }

4. Reutilizar la celda

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
    
        //这里做自定义cell重用
        var cell = tableView.dequeueReusableCellWithIdentifier("cell")

        if cell == nil {
    
    
            cell = MsgCodeCell(style: UITableViewCellStyle.Default , reuseIdentifier: "cell")
            cell!.selectionStyle = UITableViewCellSelectionStyle.None
            NSLog("初始化cell")
        }

       (cell as! MsgCodeCell).setMsgModle(array[indexPath.row])
        return cell!
    }

En este punto, la celda está terminada .

original

————————————————
Declaración de derechos de autor: Este artículo es el artículo original del blogger de CSDN "Xingyun 1992". Sigue el acuerdo de derechos de autor CC 4.0 BY-SA. Adjunte la fuente original enlace y esta declaración.
Enlace original: https://blog.csdn.net/xingyun1992/article/details/51172048

Supongo que te gusta

Origin blog.csdn.net/xmcy001122/article/details/105222734
Recomendado
Clasificación