TableView

TableView

TableView用来处理大量的数据,有以下两种方式:
这里写图片描述

一个典型的TableView由以下部分组成:
这里写图片描述

一个Table中的cell系统提供了以下四种方式:
这里写图片描述

准备工作

首先需要把storyboard清理干净,然后拖出新的tableviewcontroller。然后新建自己的TableViewController。

然后我们需要使用Twitter的framework,使用方法是把framework(工程文件)拖入项目内,但是要注意结构层次。

这里写图片描述

然后要把framework拖进去fakeTwitter的Embedded Binaries。不过可能会发现,这个framework一开始是红色的。使用之前需要使用Generic IOS Device来build一下。

然后再在使用framework的文件之前声明”import XX(framework name)”。

关键的三个函数

关于tableview最关键是如下三个函数。

override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return newstitle.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Tweet", for: indexPath)
        cell.textLabel?.text = newstitle[indexPath.row]
        cell.detailTextLabel?.text = content[indexPath.row]
        // Configure the cell...

        return cell
    }

其中里面要遍历多个元素的时候,使用indexPath.row。

这里写图片描述

但是样子还是很丑,可以使用自定义的方式来美化cell。

我们新建一个UITableViewCell文件,这个文件用来给我们创建所有View的部分。

我们需要在Storyboard里面拖好控件。然后把原本的代码改成如下的样子。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Tweet", for: indexPath)
        // Configure the cell...
        if let tweetCell = cell as? NewsTableViewCell {
            tweetCell.title = newstitle[indexPath.row]
            tweetCell.content = content[indexPath.row]
            tweetCell.date = newsdate[indexPath.row]
        }
        return cell
    }

NewTableViewCell做的事情只需要更新UI就可以了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值