【问题标题】:Custom TableViewCell with Appcelerator Hyperloop使用 Appcelerator Hyperloop 自定义 TableViewCell
【发布时间】:2017-07-07 15:18:17
【问题描述】:

您好想用 hyperloop 创建自定义 tableViewCell。问题是,我不知道该怎么做。我试过用一个快速的类来实现它:

Cell.swift

import UIKit

public class MyCell: UITableViewCell{//UICollectionViewCell {

    public var imgUser:UIImageView = UIImageView()
    public var labUerName:UILabel = UILabel()
    public var labMessage:UILabel = UILabel()
    public var labTime:UILabel = UILabel()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        NSLog("la");
        imgUser.backgroundColor = UIColor.blue

        imgUser.translatesAutoresizingMaskIntoConstraints = false
        labUerName.translatesAutoresizingMaskIntoConstraints = false
        labMessage.translatesAutoresizingMaskIntoConstraints = false
        labTime.translatesAutoresizingMaskIntoConstraints = false

        labUerName.frame = CGRect(x: 5, y: 5, width: 70, height: 30)

        contentView.addSubview(imgUser)
        contentView.addSubview(labUerName)
        contentView.addSubview(labMessage)
        contentView.addSubview(labTime)


    }

    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    public func setName(txt: String){
        labUerName.text = txt
    }


}

在我的 tabbleview 控制器中:

(function (container) {

    var UIScreen = require('UIKit/UIScreen'),
        UIColor = require('UIKit/UIColor'),
        UITableView = require('UIKit/UITableView'),
        UITableViewCell = require('UIKit/UITableViewCell'),
        NSIndexPath = require('Foundation').NSIndexPath,
        UITableViewStyleGrouped = require('UIKit').UITableViewStyleGrouped,
        UITableViewCellStyleSubtitle = require('UIKit').UITableViewCellStyleSubtitle,
        UITableViewCellAccessoryDisclosureIndicator = require('UIKit').UITableViewCellAccessoryDisclosureIndicator;

    // Grabs the JSON-file from app/lib/static/data.json 
    var file = Ti.Filesystem.getFile(Ti.Filesystem.getResourcesDirectory() + 'static/data.json'); 
    var users = JSON.parse(file.read().text).users;
    var Cell = require('MyFramework/MyCell'); // HERE IS MY CUSTOM CELL

    // Subclass delegate + data source
    var TableViewDataSourceAndDelegate = require('subclasses/tableviewdatasourcedelegate');

    // Create + configure tableView
    var tableView = UITableView.alloc().initWithFrameStyle(UIScreen.mainScreen.bounds, UITableViewStyleGrouped);
    var dataSourceDelegate = new TableViewDataSourceAndDelegate();

    dataSourceDelegate.numberOfSections = function(tableView) {
        return 1;
    };
    dataSourceDelegate.numberOfRows = function(tableView, section) {
        return users.length;
    };
    dataSourceDelegate.titleForHeader = function(tableView, section) {
        return 'Available users: ' + users.length;
    };
    dataSourceDelegate.heightForRow = function(tableView, indexPath) {
        return 44;
    };
    dataSourceDelegate.cellForRow = function(tableView, indexPath) {

        var user = users[indexPath.row];


        var cell = tableView.dequeueReusableCellWithIdentifierForIndexPath('hyperloop_cell', indexPath);



        cell.setName('abc');

        return cell;
    };
    dataSourceDelegate.didSelectRowAtIndexPath = function(tableView, indexPath) {       
        alert('Call me maybe: ' + users[indexPath.row].phone);
        tableView.deselectRowAtIndexPathAnimated(indexPath, true);
    };


    // Assign delegate + data source
    tableView.registerClassForCellReuseIdentifier(Cell.self, "hyperloop_cell");
    tableView.setDelegate(dataSourceDelegate);
    tableView.setDataSource(dataSourceDelegate);

    container.add(tableView);

})($.tableview_container);

我不知道如何使用它。有人能帮助我吗 ? 谢谢

【问题讨论】:

    标签: swift appcelerator appcelerator-titanium appcelerator-hyperloop hyperloop


    【解决方案1】:

    不知道为什么它不工作,但是你似乎没有做任何没有超级循环就无法完成的事情......为什么不直接使用 Appcelerator 创建自己的 tableview 单元格/行布局?

    【讨论】:

    • 我尝试在 JS 中实现。我不知道该怎么做。这就是我尝试使用 swift 的原因。
    • 似乎应该可以,所以也许您只需要复习一下文档?或者发布您希望该行看起来像什么,也许可以帮助您指出正确的方向。
    • 经过多次尝试,问题似乎出在我的快速课程上。我把里面的东西都清空了,一切正常……我会继续这样搜索
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多