[IOS]tableView的操作。

本文详细介绍了使用 Swift 在 TableView 中实现数据展示的方法,包括数据源设置、单元格配置及交互处理等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//
//  ViewController.swift
//  try
//
//  Created by Stary on 3/28/16.
//  Copyright © 2016 Stary. All rights reserved.
//

import UIKit

class NewTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    var array = [String](count: 50, repeatedValue: "yan")
    var arrayIsMarked = [Bool](count: 51, repeatedValue: false)
    // 必备的两个函数。
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    // 对每一个cell的操作。
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        // 每个自定义的cell都有一个identify
        let identistring = "trys"
        let cell = tableView.dequeueReusableCellWithIdentifier(identistring, forIndexPath: indexPath) as! NewTableViewCell
        cell.ClassLabel.text = "Variable"
        cell.DescriptionLabel.text = "Let's try"
        cell.NewimageView.image = UIImage(named: "p1.jpg")
        if arrayIsMarked[indexPath.row] {
            cell.accessoryType = .Checkmark
        } else {
            cell.accessoryType = .None
        }
        return cell
    }
    // 按住某个cell的反应。
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let controller = UIAlertController(title: "Thanks", message: "\(array[0])", preferredStyle: .Alert)
        let okAction = UIAlertAction(title: "Ok", style: .Default, handler: {
            (action : UIAlertAction) -> Void in
            let cell = tableView.cellForRowAtIndexPath(indexPath)
            cell?.accessoryType = UITableViewCellAccessoryType.Checkmark
            self.arrayIsMarked[indexPath.row] = true
        })
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        let alert = UIAlertAction(title: "Cancel", style: .Cancel, handler: {
            (action : UIAlertAction) -> Void in
            let cell = tableView.cellForRowAtIndexPath(indexPath)
            cell?.accessoryType = UITableViewCellAccessoryType.None
            self.arrayIsMarked[indexPath.row] = false
        })
        controller.addAction(alert)
        controller.addAction(okAction)
        self.presentViewController(controller, animated: true, completion: nil)
    }
    // 把一个cell左拉后会出现的按键。
    func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
        let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Destructive, title: "Delete", handler: {
            (action: UITableViewRowAction, NSIndexPath) -> Void in
            self.array.removeAtIndex(indexPath.row)
            self.arrayIsMarked.removeAtIndex(indexPath.row)
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Left)
        })
        let ChangeAction = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "Change") { (UITableViewRowAction, NSIndexPath) -> Void in
            let Controller = UIAlertController(title: "Do you wanna change?", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
            let YesAction = UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: {
                action in
                let controller = UIAlertController(title: "Thank you for your changing", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
                let cancelAction = UIAlertAction(title: "Bye", style: UIAlertActionStyle.Cancel, handler: nil)
                controller.addAction(cancelAction)
                self.presentViewController(controller, animated: true, completion: nil)
            })
            let NoAction = UIAlertAction(title: "No", style: .Cancel, handler: nil)
            Controller.addAction(YesAction)
            Controller.addAction(NoAction)
            self.presentViewController(Controller, animated: true, completion: nil)
        }
        return [deleteAction, ChangeAction]
    }
    // 返回每个cell的高度。
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 80
    }
}

tableView还要注意设定delegate & datasource的委托。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值