www.91084.com

GVKun编程网logo

Swift UITableView 学习(swift uistackview)

1

如果您对SwiftUITableView学习感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于SwiftUITableView学习的详细内容,我们还将为您解答swiftuist

如果您对Swift UITableView 学习感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于Swift UITableView 学习的详细内容,我们还将为您解答swift uistackview的相关问题,并且为您提供关于iOS Swift 使用以下单元格内容点击 UITableView 获取 CGPoint、iOS Swift 应用程序随机 EXC_BAD_ACCESS 崩溃:swift_bridgeObjectRetain swift_retain swift::RefCounts、ios – Swift 4:UITableView dataSource无法正常工作、ios – Swift – UITableView editActionsForRowAtIndexPath在点击编辑时打开UIPresentationController的有价值信息。

本文目录一览:

Swift UITableView 学习(swift uistackview)

Swift UITableView 学习(swift uistackview)

源码如下:

import UIKit

class TFNetimageViewController: TFBaseViewController,UITableViewDataSource,UITableViewDelegate{

    var TFTableView:UITableView!
    var itemString = ["姓名","账号","爱好","职业","年薪"]
    override func viewDidLoad() {
        super.viewDidLoad()

        self.setTopNavBarTitle("个人信息")
        self.setTopNavBackButton()
        self.view.backgroundColor = UIColor.lightGrayColor()
        self.creatTable()
    }
    
    
    func creatTable(){

        TFTableView = UITableView(frame: CGRectMake(0,64,AppWidth,AppHeight - 64),style:UITableViewStyle.Grouped);
        TFTableView.delegate = self;
        TFTableView.dataSource = self;
        self.view.addSubview(TFTableView);
    }
    
    //MARK: UITableViewDataSource
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 2;
    }
    
    func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        if(section == 0){
            return 1;
        }else{
            return 5;
        }
    }
    
    func tableView(tableView: UITableView,heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
        
        if(indexPath.section == 0){
            return 80;
        }else{
            return 55;
            
        }
    }
    
    func tableView(tableView: UITableView,heightForHeaderInSection section: Int) -> CGFloat {
        return 10;
    }
    

    func tableView(tableView: UITableView,heightForFooterInSection section: Int) -> CGFloat {
        return 1;
    }

    func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let identifier="identtifier";
        var cell=tableView.dequeueReusableCellWithIdentifier(identifier);
        if(cell == nil){
            cell=UITableViewCell(style: UITableViewCellStyle.Value1,reuseIdentifier: identifier);
        }
        
        if(indexPath.section == 0){
            cell?.textLabel?.text="头像";
        }else{
            cell?.textLabel?.text=itemString[indexPath.row];
        }
        cell?.accessoryType=UITableViewCellAccessoryType.disclosureIndicator;
        return cell!;
    }
    
    
    //MARK: UITableViewDelegate
    func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        tableView.deselectRowAtIndexPath(indexPath,animated: true);
    }

 }


效果图如下:

iOS Swift 使用以下单元格内容点击 UITableView 获取 CGPoint

iOS Swift 使用以下单元格内容点击 UITableView 获取 CGPoint

如何解决iOS Swift 使用以下单元格内容点击 UITableView 获取 CGPoint

所以单元格包含属性文本,所有内容(句子)都可以点击。(这部分工作正常。)

现在需要在屏幕上获取 CGPoint,用户点击任何内容以显示弹出菜单以提供多个选项。

我尝试过的代码,但它没有被调用,因为我有可点击的属性文本。如果我删除可点击的属性文本,则调用此函数。

  1. override func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?) {
  2. let point = touches.first?.location(in: self.view)
  3. }

iOS Swift 应用程序随机 EXC_BAD_ACCESS 崩溃:swift_bridgeObjectRetain swift_retain swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1>

iOS Swift 应用程序随机 EXC_BAD_ACCESS 崩溃:swift_bridgeObjectRetain swift_retain swift::RefCounts

如何解决iOS Swift 应用程序随机 EXC_BAD_ACCESS 崩溃:swift_bridgeObjectRetain swift_retain swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1>

我不断收到来自随机用户的随机崩溃报告。不幸的是,我无法定期重现这一点。用户说崩溃是在 discussionViewController 中随机发生的。所有崩溃报告都有类似的内容:

0   libswiftCore.dylib              0x00000001a53face4 swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1> >::incrementSlow(swift::RefCountBitsT<(swift::RefCountInlinedness)1>,unsigned int) + 60 (atomic:1003)
1   libswiftCore.dylib              0x00000001a53c59e0 swift_retain + 124 (RefCount.h:813)
2   libswiftCore.dylib              0x00000001a5401d60 swift_bridgeObjectRetain + 56 (SwiftObject.mm:585)
3   APPNAME                             0x0000000102b59734 closure #1 in discussionViewController.fetchPostData() + 7916

这是完整的崩溃日志和崩溃的线程:

Hardware Model:      iphone11,6
Process:             APPNAME [11770]
Path:                /private/var/containers/Bundle/Application/.../APPNAME.app/APPNAME
Identifier:          ----
Version:             62 (62)
AppStoretools:       12E262
AppVariant:          1:iphone11,6:13
Code Type:           ARM-64 (Native)
Role:                Foreground
Parent Process:      launchd [1]
Coalition:           ---- [1824]


Date/Time:           2021-06-17 12:07:01.4346 +1000
Launch Time:         2021-06-17 12:06:56.4993 +1000
OS Version:          iPhone OS 14.6 (18F72)
Release Type:        User
Baseband Version:    3.04.01
Report Version:      104

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x8000000000000010 -> 0x0000000000000010 (possible pointer authentication failure)
VM Region Info: 0x10 is not in any region.  Bytes before following region: 4339515376
      REGION TYPE                 START - END      [ VSIZE] PRT/MAX SHRMOD  REGION DETAIL
      UNUSED SPACE AT START
--->  
      __TEXT                   102a7c000-102a94000 [   96K] r-x/r-x SM=COW  ...APPNAME.app/APPNAME

Termination Signal: Segmentation fault: 11
Termination Reason: Namespace SIGNAL,Code 0xb
Terminating Process: exc handler [11770]
Triggered by Thread:  3


Thread 3 name:
Thread 3 Crashed:
0   libswiftCore.dylib              0x00000001a53face4 swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1> >::incrementSlow(swift::RefCountBitsT<(swift::RefCountInlinedness)1>,unsigned int) + 60 (atomic:1003)
1   libswiftCore.dylib              0x00000001a53c59e0 swift_retain + 124 (RefCount.h:813)
2   libswiftCore.dylib              0x00000001a5401d60 swift_bridgeObjectRetain + 56 (SwiftObject.mm:585)
3   APPNAME                             0x0000000102b59734 closure #1 in discussionViewController.fetchPostData() + 7916
4   APPNAME                             0x0000000102ad09d4 thunk for @escaping @callee_guaranteed (@guaranteed Data?,@guaranteed NSURLResponse?,@guaranteed Error?) -> () + 132 (<compiler-generated>:0)
5   CFNetwork                       0x00000001a1b0a3dc __40-[__NSURLSessionLocal taskForClassInfo:]_block_invoke + 540 (LocalSession.mm:687)
6   CFNetwork                       0x00000001a1b1c768 __49-[__NSCFLocalSessionTask _task_onqueue_didFinish]_block_invoke + 244 (LocalSessionTask.mm:584)
7   libdispatch.dylib               0x00000001a10d1a84 _dispatch_call_block_and_release + 32 (init.c:1466)
8   libdispatch.dylib               0x00000001a10d381c _dispatch_client_callout + 20 (object.m:559)
9   libdispatch.dylib               0x00000001a10db004 _dispatch_lane_serial_drain + 620 (inline_internal.h:2557)
10  libdispatch.dylib               0x00000001a10dbc34 _dispatch_lane_invoke + 456 (queue.c:3862)
11  libdispatch.dylib               0x00000001a10e64bc _dispatch_workloop_worker_thread + 764 (queue.c:6589)
12  libsystem_pthread.dylib         0x00000001ed04a7a4 0x1ed047000 + 14244
13  libsystem_pthread.dylib         0x00000001ed05174c 0x1ed047000 + 42828

我已验证 discussionViewController.fetchPostData() 不会强制解开任何可选选项,没有 try! 并且在任何地方都使用 [weak self]self?。该函数非常大,所以我很难缩小崩溃发生的范围。

ios – Swift 4:UITableView dataSource无法正常工作

ios – Swift 4:UITableView dataSource无法正常工作

我正在尝试将一些数据添加到UITableView对象.
我的问题是我的数据源对象中没有一个方法在运行时被调用.
我已经检查过UITableView本身是否显示.
当我调用providerTable.reloadData()时只调用numberOfSections(在tableView:UITableView中) – > Int和tableView(_ tableView:UITableView,numberOfRowsInSection section:Int) – >调用Int方法,但从不tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) – >的UITableViewCell

import UIKit

class ProviderViewController: UIViewController {

    let navigationBar = { () -> UINavigationBar in
        let bar = UINavigationBar()
        bar.setItems([
            { () -> UINavigationItem in
                let item = UINavigationItem()
                item.title = "a title"
                return item
            }()
        ],animated: false)
        bar.translatesAutoresizingMaskIntoConstraints = false
        return bar
    }()
    let providerTable = { () -> UITableView in
        let providerDataSource = ProviderTableDataSource()

        let table = UITableView()
        table.dataSource = providerDataSource
        table.translatesAutoresizingMaskIntoConstraints = false
        table.register(ProviderTableViewCell.self,forCellReuseIdentifier: "providerCell")
        return table
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.setupWindow()
    }

    func setupWindow() -> Void {
        view.backgroundColor = .white

        view.addSubview(navigationBar)
        view.addSubview(providerTable)

        // Set constraints
        var constraints = [NSLayoutConstraint]()
        // navbar
        constraints.append(NSLayoutConstraint(item: navigationBar,attribute: .width,relatedBy: .equal,toItem: view,multiplier: 1,constant: 0))
        constraints.append(NSLayoutConstraint(item: navigationBar,attribute: .left,attribute: .top,toItem: view.safeAreaLayoutGuide,constant: 0))
        // provider table
        constraints.append(NSLayoutConstraint(item: providerTable,constant: 0))
        constraints.append(NSLayoutConstraint(item: providerTable,toItem: navigationBar,attribute: .bottom,constant: 0))
        // activate constraints
        view.addConstraints(constraints)
    }

}

数据源对象:
    导入UIKit

class ProviderTableDataSource: NSObject,UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "providerCell",for: indexPath) as! ProviderTableViewCell
        cell.charterProvider = CharterProviders.providers[indexPath.row]

        return cell
    }
}

解决方法

您没有对创建表视图时创建的ProviderTableDataSource保持强引用:

let providerTable = { () -> UITableView in
    let providerDataSource = ProviderTableDataSource()

    let table = UITableView()
    table.dataSource = providerDataSource
    table.translatesAutoresizingMaskIntoConstraints = false
    table.register(ProviderTableViewCell.self,forCellReuseIdentifier: "providerCell")
    return table
}()

UITableView的dataSource属性很弱,因此您不能依赖它来保存对ProviderTableDataSource的强引用.请参阅此处的文档:
https://developer.apple.com/documentation/uikit/uitableview/1614955-datasource?changes=_6

你想做的可能是这样的:

class ProviderViewController: UIViewController {
    private let providerDataSource = ProviderTableDataSource()

    private(set) lazy var providerTable: UITableView = {
        let table = UITableView()
        table.dataSource = self.providerDataSource
        table.translatesAutoresizingMaskIntoConstraints = false
        table.register(ProviderTableViewCell.self,forCellReuseIdentifier: "providerCell")
        return table
    }()

    // [...]
}

这样,视图控制器持有对ProviderTableDataSource的强引用,因此在创建表视图后不会释放它.

ios – Swift – UITableView editActionsForRowAtIndexPath在点击编辑时打开UIPresentationController

ios – Swift – UITableView editActionsForRowAtIndexPath在点击编辑时打开UIPresentationController

您是否有任何方式打开一个UIPresentationController,当向左滑动触发并点击编辑?
func tableView(tableView: UITableView,editactionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
        let delete = ....
        let edit = UITableViewRowAction(style: .normal,title: "Edit") { action,index in
            //OPEN UIPresentationController HERE
        }
        return [delete,edit]
}

解决方法

和@patchdiaz一样,我不是100%肯定你想做什么.但是,这个代码块可能足以实现您的目标:
override func tableView(tableView: UITableView,editactionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
    let edit = UITableViewRowAction(style: .normal,index in
        // OPEN UIPresentationController HERE
        let vc = UIViewController(nibName: nil,bundle: nil)
        vc.view.frame = CGRect(x: 0,y: 0,width: 100,height: 200)
        vc.view.backgroundColor = UIColor.orangeColor()
        vc.modalPresentationStyle = .Popover

        let popover = vc.popoverPresentationController!
        let cell = tableView.cellForRowAtIndexPath(indexPath)!

        var cellAbsolutePosition = cell.superview!.convertPoint(cell.frame.origin,toView: nil)
        cellAbsolutePosition.x = cell.frame.width - 60
        popover.sourceRect = CGRect(origin: cellAbsolutePosition,size: cell.frame.size)
        popover.sourceView = tableView

        self.presentViewController(vc,animated: true,completion: nil)
    }
    return [edit]
}

它会在“编辑”按钮位置显示一个popover权限,如下所示:

关于Swift UITableView 学习swift uistackview的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于iOS Swift 使用以下单元格内容点击 UITableView 获取 CGPoint、iOS Swift 应用程序随机 EXC_BAD_ACCESS 崩溃:swift_bridgeObjectRetain swift_retain swift::RefCounts、ios – Swift 4:UITableView dataSource无法正常工作、ios – Swift – UITableView editActionsForRowAtIndexPath在点击编辑时打开UIPresentationController的相关知识,请在本站寻找。

本文标签: