小编Muh*_*med的帖子

试图在Swift中传递Generic类型作为参数

我正在使用AlamofireObjectMapper 我需要创建一个带有这样的Generic参数的函数:

func doSomething < T : BaseMappable > (myCustomClass : T) 
{
    Alamofire.request("url", method: .get, parameters: nil, encoding: JSONEncoding.default, headers: APIKeys().AuthorizedHeader).responseObject(completionHandler: { ( response :DataResponse<T>) in

             let data = response.result.value

            if let array = data?.objects
            {
                for ar in array
                {
                   self.allPromotions.append(ar)
                }
            }

        })


}
Run Code Online (Sandbox Code Playgroud)

但我得到错误:

使用未声明的类型'myCustomClass' 编辑, 因为你们在评论中回答我我修复了错误但是当我试图调用这个方法时我得到了另一个错误

我这样称呼方法

doSomething(myCustomClass: Promotions)
Run Code Online (Sandbox Code Playgroud)

但我有另一个错误

参数类型'Promotions.Type'不符合预期类型'BaseMappable'

这是我的促销课程

import ObjectMapper


class Promotions : Mappable  {





        var id:Int?
        var workshop_id:Int?
        var title:String?
        var desc:String?
        var start_date:String?
        var expire_date:String?
        var type:String?

        var …
Run Code Online (Sandbox Code Playgroud)

generics swift alamofire

5
推荐指数
3
解决办法
4519
查看次数

在UIWebView swift中单击按钮时打开ViewController

我有一个拥有UIWebView的应用程序,而Webview包含简单的Button

这是我的WebViewController:

import UIKit

class testViewController: UIViewController {
    internal static var testID = 0

    @IBOutlet weak var myWebView: UIWebView!
    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        let url = NSURL (string: "http://mydomainname/index.html");
        let requestObj = NSURLRequest(URL: url!);
        myWebView.loadRequest(requestObj);
    }
}
Run Code Online (Sandbox Code Playgroud)

并且webView完美地显示了我的HTML

当我单击HTML文件中的按钮时,我只需要从我的iOS项目中呈现一个newViewController?

像那样 :

<button onclick="myFunction()> Submit </button>

<script>
function myFunction()
{
presentViewController('myViewControllerName');

}



</script>
Run Code Online (Sandbox Code Playgroud)

在iOS中有什么办法吗?

html javascript webview ios swift

3
推荐指数
1
解决办法
3649
查看次数

tableView heightForRowAtIndexPath不起作用

我有表视图来显示用户的评论

我需要根据内容高度使每行的高度动态化

我搜索了它,我发现了

heightForRowAtIndexPath方法

但它不起作用或我不知道如何使用它!

这是我的代码:

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
   let username = cell.viewWithTag(1) as! UILabel

    let comment = cell.viewWithTag(2) as! UITextView
    username.text = usernames[indexPath.row]

    comment.text = comments[indexPath.row]

    return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.comments.count
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension;
}
Run Code Online (Sandbox Code Playgroud)

uitableview ios swift

1
推荐指数
1
解决办法
6590
查看次数

为什么向 UICollectionViewCell 中的按钮添加动作效果不佳?

我有 CollectionView 有多个动态单元格,foreach 单元格有按钮,它具有添加项目数的操作,这是我的简单代码:

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    if ids.count == 0
    {
        return 3
    }else
    {
    return ids.count
    }

}




func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {


    if ids.count == 0
    {
        let cell = myCollection.dequeueReusableCellWithReuseIdentifier("loadingItems", forIndexPath: indexPath)

        return cell


    }else
    {
  let  cell =myCollection.dequeueReusableCellWithReuseIdentifier("cellProduct", forIndexPath: indexPath) as! productsCollectionViewCell

      cell.addItems.addTarget(self, action: #selector(homeViewController.addItemsNumberToCart(_:)), forControlEvents: UIControlEvents.TouchUpInside) 
  }







    return cell

    }
}
Run Code Online (Sandbox Code Playgroud)

这是添加项目的方法

func addItemsNumberToCart(sender:UIButton)
{

sender.setTitle("Added to cart", forState: UIControlState.Normal)

}
Run Code Online (Sandbox Code Playgroud)

这是我的 collectionViewCell 类 …

uibutton uicollectionview uicollectionviewcell swift

1
推荐指数
1
解决办法
1953
查看次数

searchBarSearchButtonClicked 不起作用 Swift

我正在尝试使用 UISearchBar 但它对我不起作用

这是我的代码

class searchViewController: UIViewController  , UITableViewDataSource , UISearchBarDelegate{
    var searchResults = [String]()
    @IBOutlet weak var searchTable: UITableView!
    @IBOutlet weak var mySearchBar: UISearchBar!
    override func viewDidLoad() {
        super.viewDidLoad()


    }



    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return searchResults.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = searchTable.dequeueReusableCellWithIdentifier("searchCell", forIndexPath: indexPath) as! UITableViewCell


        return cell
    }
    func searchBarSearchButtonClicked(searchBar: UISearchBar) {
        print("print anything ")
    }

    func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
        print(self.mySearchBar.text)
    }



}
Run Code Online (Sandbox Code Playgroud)

当我使用模拟器单击键盘中的搜索按钮时,没有发生任何事情

我试过 …

uisearchbar swift

0
推荐指数
1
解决办法
4870
查看次数