【问题标题】:Prevent "....." when displaying json data into tableview将json数据显示到tableview中时防止“.....”
【发布时间】:2016-05-03 19:57:44
【问题描述】:

我正在尝试显示 josn 数据并且发生了这种情况

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

这里是代码

这是显示问题所在的图片 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
        })
    }









}

【问题讨论】:

  • 您是否在寻找要换行到下一行的文本?
  • 请尽可能少使用仍然会产生相同问题的代码。避免包含明显与问题无关的代码。

标签: ios json swift uitableview


【解决方案1】:

您可以更改要在 tableview 单元格上显示的 textLabel 的行数,如下所示。 通过将其设置为“0”,它将显示全文,不知道它有多长。如果你想要修复行,然后包装内容。您可以设置除 0 以外的数字。

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

【讨论】:

    【解决方案2】:

    您可以使用 tableview 单元格的自动尺寸来做到这一点。请关注此网址。他提到了textview。你可以对标签做同样的事情。

    Change height of textview according to content

    【讨论】:

      【解决方案3】:

      假设您的文本在 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
      }
      

      上面我还设置了最小字体大小,这样就不会小到你看不到了。

      【讨论】:

      • 它在表格视图单元格中
      • 你想要完整的项目吗?
      • 假设它是一个基本的表格视图单元格,那么您应该能够在其 textLabel 属性上设置该属性,因为它是一个 UILabel。
      • 我是新手,我该怎么做?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-28
      • 2015-08-17
      • 1970-01-01
      • 2018-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多