gpt4 book ai didi

ios - 在 Swift 中选择时关闭 UITableView Cell?

转载 作者:行者123 更新时间:2023-11-29 01:48:43 30 4
gpt4 key购买 nike

我在 View Controller 中有一个 UITableView 单元格,用作文本字段自动完成的显示。但是,选择其中一个自动完成建议不会关闭表格 View 单元格。我在 UITableView 的 didSelectRowAtIndexPath 函数中尝试了“self.dismissViewControllerAnimated(True, completion: nil)”。有什么想法吗?

class ViewController: UIViewController, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var RecName: UITextField!
@IBOutlet var Body: UITextField!
@IBOutlet var RecEmail: UITextField!

var emailArray = ""
var emailNSArray = [""]
var autocomplete = [String]()
var tycard2 = ""
var addressBook: ABAddressBookRef?
@IBOutlet var autocompleteTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
//email
RecEmail.delegate = self
//autocomplete
autocompleteTableView!.delegate = self
autocompleteTableView!.dataSource = self
autocompleteTableView!.scrollEnabled = true
autocompleteTableView!.hidden = true
println(tycard2)
}

func createAddressBook(){
var error: Unmanaged<CFError>?
addressBook = ABAddressBookCreateWithOptions(nil, &error).takeRetainedValue()
}


func textToImage(drawText: NSString, inImage: UIImage, atPoint:CGPoint)->UIImage{

// Setup the font specific variables
var textColor: UIColor = UIColor.whiteColor()
var textFont: UIFont = UIFont(name: "Helvetica Bold", size: 20)!

//Setup the image context using the passed image.
UIGraphicsBeginImageContext(inImage.size)

//Setups up the font attributes that will be later used to dictate how the text should be drawn
let textFontAttributes = [
NSFontAttributeName: textFont,
NSForegroundColorAttributeName: textColor,
]

//Put the image into a rectangle as large as the original image.
inImage.drawInRect(CGRectMake(0, 0, inImage.size.width, inImage.size.height))

// Creating a point within the space that is as bit as the image.
var rect: CGRect = CGRectMake(atPoint.x, atPoint.y, inImage.size.width, inImage.size.height)

//Now Draw the text into an image.
drawText.drawInRect(rect, withAttributes: textFontAttributes)

// Create a new image out of the images we have created
var newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()

// End the context now that we have the image we need
UIGraphicsEndImageContext()

//And pass it back up to the caller.
return newImage

}


func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
autocompleteTableView!.hidden = false
var substring = (self.RecEmail.text as NSString).stringByReplacingCharactersInRange(range, withString: string)

searchAutocompleteEntriesWithSubstring(substring)
self.dismissViewControllerAnimated(true, completion: {})
return true
}

func searchAutocompleteEntriesWithSubstring(substring: String)
{
autocomplete.removeAll(keepCapacity: false)
let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
let emailArray = delegate.emailArray
var emailNSArray = emailArray.componentsSeparatedByString(",")

for curString in emailNSArray
{
println(curString)
var myString: NSString! = curString as NSString
var substringRange: NSRange! = myString.rangeOfString(substring)
if (substringRange.location == 0)
{
autocomplete.append(curString)
}
}

autocompleteTableView!.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return autocomplete.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let autoCompleteRowIdentifier = "AutoCompleteRowIdentifier"
var cell = tableView.dequeueReusableCellWithIdentifier(autoCompleteRowIdentifier) as? UITableViewCell

if let tempo1 = cell
{
let index = indexPath.row as Int
cell!.textLabel!.text = autocomplete[index]
} else
{
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: autoCompleteRowIdentifier)
}
return cell!
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedCell : UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
self.dismissViewControllerAnimated(true, completion: nil)
println("dismiss")
RecEmail.text = selectedCell.textLabel!.text

}

谢谢!

最佳答案

您正试图关闭 ViewController 而不是表格 View 单元格。如果您想删除一个单元格,只需删除数据源中的当前行索引(这里是您的 [String] 数组 autocomplete),然后重新加载数据。

关于ios - 在 Swift 中选择时关闭 UITableView Cell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31645324/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com