Day 2 - TableView

先上效果圖

效果圖

1、搭建UI

新建工程后,在Main.Stroyboard中拖入一個(gè)TableView和一個(gè)Button,設(shè)置約束,顏色后如下:


UI效果圖

2、拖線

先拖屬性


屬性

依次拖線


tableVIew,button

最后在為button添加Action、


button

OK 布局到這里就完成了

3、創(chuàng)建數(shù)據(jù)源、設(shè)置代理、實(shí)現(xiàn)代理方法,設(shè)置按鈕樣式

  • 3.1 創(chuàng)建數(shù)據(jù)源
var data : Array? = ["Day 2 TableView", "每天學(xué)習(xí)多一點(diǎn)", "每天累積一點(diǎn)點(diǎn)", "暗號(hào):天王蓋地虎", "?? ?? ??See you next Project", "Learning Swift","紙巾藝術(shù)","參考網(wǎng)絡(luò)學(xué)習(xí)資料"]
解釋:創(chuàng)建了一個(gè)變量名為data,類型為數(shù)組的數(shù)據(jù)源
  • 3.2 設(shè)置代理
tableView.dataSource = self
tableView.delegate = self

這個(gè)時(shí)候會(huì)報(bào)錯(cuò),找不到delegate、dataSource
導(dǎo)入代理

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource 

這個(gè)時(shí)候還是會(huì)報(bào)錯(cuò),因?yàn)闆]有實(shí)現(xiàn)代理

  • 3.3 實(shí)現(xiàn)代理
    在tableView中必須實(shí)現(xiàn)的代理有兩個(gè):
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data!.count
    }

第一個(gè)在某個(gè)section內(nèi)有多少個(gè)Cell,如果不實(shí)現(xiàn)
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        這里返回有多少個(gè)section
    }
return data!.count 返回data的個(gè)數(shù) 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier("FontCell", forIndexPath: indexPath)
        
        let text = data![indexPath.row]
        
        cell.backgroundColor? = UIColor.blackColor();
        cell.textLabel?.text = text
        cell.textLabel?.textColor = UIColor.whiteColor()
        
        return cell
        
    }
line 1:let cell... 這個(gè)是用復(fù)用池中獲取cell 是用這個(gè)方法必須要提前注冊(cè),否則會(huì)報(bào)錯(cuò)
所以還需要在設(shè)置Viewdidload中寫上:
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "FontCell"); 
這里注意Identifier這個(gè)參數(shù)名中的字符串要保持一致
let text:這里從數(shù)組中獲取數(shù)據(jù)
back..Color:設(shè)置背景顏色
t..Label.text :設(shè)置文字
...textColor:文字顏色

3.4 設(shè)置按鈕樣式

changeBtn.layer.cornerRadius = 50
這里設(shè)置成為你按鈕寬度的一半,寬高保持一致
比如寬高 100 100 那么久設(shè)置 50

<br />
此時(shí)你運(yùn)行就能看到效果了,但是你點(diǎn)擊按鈕還不能改變字體
<br />

4、改變字體

我們并不知道有哪些字體,系統(tǒng)提供的字體很多,我們可以用過一個(gè)方法遍歷出來

for family in UIFont.familyNames() {
            for font in UIFont.fontNamesForFamilyName(family){
                print(font)
            }
        }

這個(gè)for循環(huán)能把所有的字體名稱遍歷出來,我們只要從里面隨便抽取幾個(gè)名稱組成一個(gè)數(shù)組,然后每次點(diǎn)擊對(duì)tableView.cell設(shè)置不同的字體,并且刷新就能看到效果了

var fontNames = ["MFJinHei_Noncommercial-Regular", "PingFangSC-Thin", "KhmerSangamMN","Baskerville-SemiBold","Cochin-BoldItalic"]

在設(shè)置顏色后面添加

在申明兩個(gè)變量

var fontRowIndex = 0
    
var add = 0
....textColor =  UIColor.whiteColor()
cell.textLabel?.font = UIFont(name:self.fontNames[fontRowIndex], size:20)

現(xiàn)在只需要在按鈕的點(diǎn)擊事件中處理就行了

@IBAction func change(sender: UIButton) {
        
        add += 1
        fontRowIndex = add % fontNames.count;
        tableView.reloadData();
    }
tableView.reloadData();刷新

到這里就完成啦,趕緊運(yùn)行看一看吧!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容