在将json数据显示到tableview时防止“.....”

时间:2016-01-27 02:14:21

标签: ios json swift uitableview

我试图显示josn数据并且发生这种情况

如何防止......出现并显示全文?

这是代码

这是Pic显示问题所在 Pic

正如您所看到的那样,“...”并没有显示全文

对此有何解决方法?

由于

    [{"url":"","description":"rferferferferferferferferferferferferferferferferfreferferferferferferferferfeerfeferferferf"},
{"url":"","description":"Image Description"},
{"url":"","description":"Image Description"},
{"url":"","description":"Image Descriprffttion"},
{"url":"","description":"Image Descriptijijion"},
{"url":"","description":"Image Description"},

{"url":"","description":"techavindu"},
{"url":"","description":"Image Description"},
{"url":"","description":"Imagesdaasdsd Description"},
{"url":"","description":"Image Descriprffttion"},
{"url":"","description":"Image Descriptijijion"},
{"url":"","description":"Image Dyubuyuubububububububuescription"},

{"url":"","description":"techavindu"},
{"url":"","description":"Image Description"},
{"url":"","description":"Imagesdaasdsd Description"},
{"url":"","description":"Image Descriprffttion"},
{"url":"","description":"Image Descriptijijion"},





]

 var json_data_url = "http://aliectronics.com.au/json_table_view_images%20(1).json"


    var isProgressShowing = true;

    var TableData:Array< datastruct > = Array < datastruct >()

    enum ErrorHandler:ErrorType
    {
        case ErrorFetchingResults
    }


    struct datastruct
    {

        var description:String?


        init(add: NSDictionary)
        {

            description = add["description"] as? String



        }

    }

    @IBOutlet var tableview: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = color125



        tableview.dataSource = self
        tableview.delegate = self

        get_data_from_url(json_data_url)
    }




    override func viewWillAppear(animated: Bool) {
        let barButtonItem = UIBarButtonItem(title: "Refresh", style: .Plain, target: self, action: "refreshTapped");
        self.navigationItem.rightBarButtonItem = barButtonItem;
    }



    func refreshTapped() {
        addProgressIndicator(isProgressShowing);
        get_data_from_url(json_data_url)

    }

    func addProgressIndicator(show : Bool) {
        isProgressShowing = !show;
        if(show) {
            let myActivityIndicator = UIActivityIndicatorView(activityIndicatorStyle:UIActivityIndicatorViewStyle.Gray)
            myActivityIndicator.startAnimating()
            let barButtonItem = UIBarButtonItem(customView: myActivityIndicator)
            self.navigationItem.rightBarButtonItem = barButtonItem




        } else {
            self.navigationItem.rightBarButtonItem = nil;




        }




    }




    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

        let data = TableData[indexPath.row]


        cell.textLabel?.text = data.description



        return cell

    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return TableData.count
    }







    func get_data_from_url(url:String)
    {


        let url:NSURL = NSURL(string: url)!
        let session = NSURLSession.sharedSession()

        let request = NSMutableURLRequest(URL: url)
        request.HTTPMethod = "GET"
        request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData


        let task = session.dataTaskWithRequest(request) {
            (
            let data, let response, let error) in

            guard let _:NSData = data, let _:NSURLResponse = response where error == nil else {
                print("error")
                return
            }

            dispatch_async(dispatch_get_main_queue(), {
                self.extract_json(data!)
                return
            })

        }
        addProgressIndicator(!isProgressShowing);
        task.resume()



    }


    func extract_json(jsonData:NSData)
    {
        let json: AnyObject?
        do {
            json = try NSJSONSerialization.JSONObjectWithData(jsonData, options: [])
        } catch {
            json = nil
            return
        }

        if let list = json as? NSArray
        {
            for (var i = 0; i < list.count ; i++ )
            {
                if let data_block = list[i] as? NSDictionary
                {

                    TableData.append(datastruct(add: data_block))
                }
            }


            do_table_refresh()

        }


    }




    func do_table_refresh()


    {
        dispatch_async(dispatch_get_main_queue(), {
            self.tableview.reloadData()



            return
        })
    }









}

3 个答案:

答案 0 :(得分:2)

您可以使用自动维度为tableview的单元格执行此操作。请关注此网址。他提到了textview。你可以用标签做同样的事。

Change height of textview according to content

答案 1 :(得分:1)

假设您的文字位于UILabel,您可以按以下方式使用adjustsFontSizeToFitWidth

myLabel.adjustsFontSizeToFitWidth = true

查看文档here。鉴于您使用的是UITableViewCell,您可以执行以下操作:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
    let data = TableData[indexPath.row]
    cell.textLabel?.text = data.description
    cell.textLabel!.minimumFontSize = 8
    cell.textLabel!.adjustsFontSizeToFitWidth = true

    return cell
}

我还设置了上面的最小字体大小,所以它不会变得那么小,你看不到它。

答案 2 :(得分:0)

您可以更改要在tableview单元格上显示的textLabel行数,如下所示。 通过将其设置为&#34; 0&#34;它会显示全文,没有电表显示多长时间。如果你想要修复行,那么包装内容之后。您可以设置0以外的数字。

   func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
    ....
    }