GVKun编程网logo

Swift中的UITableView(SWIFT中的MT+数字有哪些)

11

关于Swift中的UITableView和SWIFT中的MT+数字有哪些的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于ios–Swift:删除图层,而不删除UITableViewCell

关于Swift中的UITableViewSWIFT中的MT+数字有哪些的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于ios – Swift:删除图层,而不删除UITableViewCell中的UIView、ios – Swift:如何在UITableViewCell中创建可点击的UIView?、ios – UITableView-Swift:UITableViewCell中的Checkmark、ios – 为什么在Swift中发生“使用未声明的UITableView”?等相关知识的信息别忘了在本站进行查找喔。

本文目录一览:

Swift中的UITableView(SWIFT中的MT+数字有哪些)

Swift中的UITableView(SWIFT中的MT+数字有哪些)

我正在努力弄清楚此代码段出了什么问题。目前这在Objective-
C中有效,但是在Swift中这只是在方法的第一行崩溃。它在控制台日志中显示错误消息:Bad_Instruction

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!  {        var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell        if (cell == nil) {            cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "Cell")        }        cell.textLabel.text = "TEXT"        cell.detailTextLabel.text = "DETAIL TEXT"        return cell    }

答案1

小编典典

另请参阅马特的答案,其中包含解决方案的后半部分

让我们找到一个无需创建自定义子类或笔尖的解决方案

真正的问题在于,Swift区分可以为空(nil)的对象和不能为空的对象。如果您没有为标识符注册笔尖,则dequeueReusableCellWithIdentifier可以返回nil

这意味着我们必须将变量声明为可选的:

var cell : UITableViewCell?

而且我们必须使用as?notas

//variable type is inferredvar cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCellif cell == nil {    cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")}// we know that cell is not empty now so we use ! to force unwrapping but you could also define cell as// let cell = (tableView.dequeue... as? UITableViewCell) ?? UITableViewCell(style: ...)cell!.textLabel.text = "Baking Soda"cell!.detailTextLabel.text = "1/2 cup"cell!.textLabel.text = "Hello World"return cell

ios – Swift:删除图层,而不删除UITableViewCell中的UIView

ios – Swift:删除图层,而不删除UITableViewCell中的UIView

我有UITableViewCell和UIView.
我做了一些CABasicAnimation并将它附加到新的CAShapeLayer,然后我将这个图层添加到我的UITableViewCell中的超级图层:
self.layer.addSublayer(myLayer!)

除了myLayer(和他的动画)显示在我的UIView之上之外,一切都很好.
我希望那个标签在UIView下面.

我通过以相同的方式添加我的UIView层来实现这一点:

self.layer.addSublayer(myViewLayer!)

在这种情况下,我的UIView图层位于带有动画的CAShapeLayer的顶部.

但我有一个问题,我需要删除UIView层 – myViewLayer,因为它在滚动时违反了UIView的宽度.

当动画完成后,我需要删除图层,我可以毫无困难地删除CAShapeLayer – myLayer.

myLayer?.removeFromSuperlayer()

但是当我尝试对myViewLayer做同样的事情时,我的UIView也被删除了.但我不希望这样,我需要在屏幕上和我的视图层次结构中使用我的UIView.

我不明白为什么,如果看看self.layer.sublayers我看到这个(在添加图层之前):

[<CALayer: 0x7fd1f0d4fe40>,<CALayer: 0x7fd1f0ddc950>]

完成动画并移除myLayer后:

[<CALayer: 0x7fd1f0d4fe40>,<CALayer: 0x7fd1f0ddc950>,<CAGradientLayer: 0x7fd1f0de3660>]

正如你所看到的,CAGradientLayer是我的UIView层.所以,在我手动将其添加到子图层数组之前,我还没有.我如何删除它,而不删除我的UIView?

或者如何在UIView下面添加myLayer?

编辑

简而言之,我在动画之前有这个图层层次结构:

[<CALayer: 0x7fd1f0d4fe40> <- (I think this is UITableViewCell layer),<CALayer: 0x7fd1f0ddc950> <- (then,this is UITableViewCell content view layer)]

同时,我有这个视图层次结构:

UITableViewCell – >内容视图 – >的UIView

我需要在UIView下面添加带动画的新图层(我希望UIView部分覆盖带动画的新图层).我怎么能这样做?

我在UITableView图层层次结构中没有我的UIView图层,所以我不能只使用addLayer添加带动画的图层:下面:依此类推.

解决方法

我发现了这个问题.我犯了一个简单的错误.我使用了self.layer.sublayers,但我必须使用self.contentView.layer.sublayers这是我的错误.

当我发现这一点时,我能够修复另一个问题并使用addLayer:下面:

因此,如果您没有看到必须位于对象子层中的图层,则意味着您在错误的位置查看它也可以找到它,就像在UITableView中一样,您需要使用contentView.

ios – Swift:如何在UITableViewCell中创建可点击的UIView?

ios – Swift:如何在UITableViewCell中创建可点击的UIView?

在UITableViewCell内部,我试图实现一个包含图像和文本的按钮.

似乎标准的UIButton无法实现这一点.所以我创建了一个包含UIImageView和UILabel的UIView.

您可以在右侧看到实现,“跟随行程”按钮(“”是UIImageView,“跟随行程”是UILabel)

我现在正试图使这样的UIView(即按钮)可点击,但我找不到办法.

这是我的实现,但它不起作用:

class StationsIntroHeader: UITableViewCell {

    @IBOutlet weak var bigButton: UIView!

    override  func awakeFromNib() {
        super.awakeFromNib()
        let tap = UITapGestureRecognizer(target: self,action: Selector("followTrip:"))
        bigButton.addGestureRecognizer(tap)
    }

    func followTrip(sender:UITapGestureRecognizer) {
        print("tap working")
    }
}

我确保在UIVmageView和UILabel上的UIView和OFF上启用了User Interaction Enabled

解决方法

对我来说,如下所示的示例设置完全有效:
class TableViewController: UITableViewController {
  override func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
    return 5
  }

  override func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    return tableView.dequeueReusableCellWithIdentifier("CustomCell",forIndexPath: indexPath)
  }
}

class CustomCell: UITableViewCell {
  @IBOutlet weak var bigButton: UIView!

  override func awakeFromNib() {
    super.awakeFromNib()
    let tap = UITapGestureRecognizer(target: self,action: Selector("bigButtonTapped:"))
    bigButton.addGestureRecognizer(tap)
  }

  func bigButtonTapped(sender: UITapGestureRecognizer) {
    print("bigButtonTapped")
  }
}

我没有为视图或imageview或标签更改userInteractionEnabled的任何默认值.将我的实施与你的实施进行比较,看看你是否忘记了某些事情…连接插座?

示例项目:https://www.dropbox.com/sh/hpetivhc3gfrapf/AAAf6aJ0zhvRINPFJHD-iMvya?dl=0

编辑您的项目

func tableView(tableView: UITableView,viewForHeaderInSection section: Int) -> UIView? {
  let headerCell = tableView.dequeueReusableCellWithIdentifier("StationsIntroHeader") as! StationsIntroHeader
  headerCell.update()
  return headerCell
  // return headerCell.update().contentView
}

ios – UITableView-Swift:UITableViewCell中的Checkmark

ios – UITableView-Swift:UITableViewCell中的Checkmark

我正在使用 Xcode 6.0.1,iOS8和 Swift.
我必须在我的UITableView中重现与Settings-> General中的auto-lock选项相同的行为.在Objective-C中,我写过

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static Nsstring *checkmarkCellIdentifier = @"checkmarkCellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: checkmarkCellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:checkmarkCellIdentifier] autorelease];
    }
    cell.textLabel.text = ...
    cell.detailTextLabel.text = ...;
    cell.accessoryType = [indexPath isEqual: self.lastSelectedindexPath] ? UITableViewCellAccessorycheckmark : UITableViewCellAccessoryNone;  
    return cell;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    int newRow = indexPath.row;
    int oldRow  = self.lastSelectedindexPath.row;
    if (newRow != oldRow) {
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
        newCell.accessoryType = UITableViewCellAccessorycheckmark;
        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.lastSelectedindexPath];
        oldCell.accessoryType = UITableViewCellAccessoryNone;
        self.lastSelectedindexPath = indexPath;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

这很棒!
现在,在Swift中,我写道:

override func viewDidLoad() {
    super.viewDidLoad()
    ...
    self.tableView.registerClass(UITableViewCell.self,forCellReuseIdentifier: "categoryCell")
}

func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("categoryCell",forIndexPath: indexPath) as UITableViewCell
    cell.textLabel?.text = categories[indexPath.row]
    let row = indexPath.row;
    if let lastIndexPath = self.lastSelectedindexPath {
        cell.accessoryType = (lastIndexPath.row == row) ? UITableViewCellAccessoryType.checkmark : UITableViewCellAccessoryType.None;
    }
    return cell
}

func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.tableView.deselectRowAtIndexPath(indexPath,animated: true)

    let newRow = indexPath.row;
    var oldRow: Int?
    if let lastIndexPath = self.lastSelectedindexPath {
        oldRow = lastIndexPath.row;
        let oldCell = self.tableView(tableView,cellForRowAtIndexPath: lastIndexPath)
        oldCell.accessoryType = UITableViewCellAccessoryType.None;
    }
    if (newRow != oldRow) {
        let newCell = self.tableView(tableView,cellForRowAtIndexPath: indexPath)
        newCell.accessoryType = UITableViewCellAccessoryType.checkmark;
        self.lastSelectedindexPath = indexPath;
    }
}

这不起作用.复选标记有时仅出现,当我滚动时它会消失.我花了两个小时才弄明白为什么但没有成功.

解决方法

主要问题是你调用tableView(_:cellForRowAtIndexPath :)而不是tableView.cellForRowAtIndexPath(_ :),因此创建一个新的单元格而不是获取当前显示的单元格.

这是一些清理:

override func viewDidLoad() {
    super.viewDidLoad()

    // …

    tableView.registerClass(UITableViewCell.self,forCellReuseIdentifier: "categoryCell")
}


func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("categoryCell",forIndexPath: indexPath) as UITableViewCell
    cell.accessoryType = (lastSelectedindexPath?.row == indexPath.row) ? .checkmark : .None
    cell.textLabel?.text = categories[indexPath.row]

    return cell
}


func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath,animated: true)

    if indexPath.row != lastSelectedindexPath?.row {
        if let lastSelectedindexPath = lastSelectedindexPath {
            let oldCell = tableView.cellForRowAtIndexPath(lastSelectedindexPath)
            oldCell?.accessoryType = .None
        }

        let newCell = tableView.cellForRowAtIndexPath(indexPath)
        newCell?.accessoryType = .checkmark

        lastSelectedindexPath = indexPath
    }
}

ios – 为什么在Swift中发生“使用未声明的UITableView”?

ios – 为什么在Swift中发生“使用未声明的UITableView”?

我是 Swift的新手.我创建了一个第二个swift文件调用DeviceInfo.swift并在Main.storyboard中添加一个UIViewcontroller.

我还在UIViewcontroller(DeviceInfo)中添加了一个UITableView.

但是通过命令左边的UITableView连接到DeviceInfo.swift后,错误的使用未声明的类型’UITableView’显示.错误显示如下.

为什么使用未申报类型的“UITableView”发生了?如何解决?

2.显示我连接数据源并委托给UIViewcontroller(DeviceInfo)?

提前致谢.

解决方法

导入UIKit

要连接delegate和dataSource,单击该点并拖动到您的tableView.

今天关于Swift中的UITableViewSWIFT中的MT+数字有哪些的分享就到这里,希望大家有所收获,若想了解更多关于ios – Swift:删除图层,而不删除UITableViewCell中的UIView、ios – Swift:如何在UITableViewCell中创建可点击的UIView?、ios – UITableView-Swift:UITableViewCell中的Checkmark、ios – 为什么在Swift中发生“使用未声明的UITableView”?等相关知识,可以在本站进行查询。

本文标签: