도래울

swift UITableView 예제 본문

개발/iOS

swift UITableView 예제

도래울 2016. 4. 21. 11:33

In Swift there are no separate header and implementation files, the whole View Controller class is defined in the ViewController.swift file. Go to this file and right after the class ViewController: UIViewController {  line add a constant containing some data for the table rows.

let tableData = ["One","Two",Three"]

Swift has inferred the constant as an Array of Strings. Next, set the number of rows in the tableView:numberOfRowsInSection function. Add this function before the closing bracket of the class.

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int 
{    
  return self.tableData.count;
}

Our Table View will have 3 rows according to the count method of the Array class. Note in Swift a class method is called using the dot syntax. Next, implement the tableView:cellForRowAtIndexPath function

func tableView(tableView: UITableView!,
        cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{   
    let cell:UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Default, reuseIdentifier:"cell")      
    cell.textLabel?.text = tableData[indexPath.row]
                
    return cell 
}


'개발 > iOS' 카테고리의 다른 글

UIScrollview Delegate 종류  (0) 2016.04.22
swift ActionSheet 예제  (0) 2016.04.21
아이튠즈 커넥트 앱 상태 정보  (0) 2016.04.15
Xcode 플러그인 관리툴 – Alcatraz Package Manager  (0) 2016.04.12
swift network library alamofire  (1) 2016.04.07
Comments