我在视图控制器中有一个UITableView单元,用作文本字段自动完成的显示。但是,选择其中一个自动填充建议不会忽略表格视图单元格。我试过" self.dismissViewControllerAnimated(True,completion:nil)"在我的UITableView的didSelectRowAtIndexPath函数中。有什么想法吗?
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
}
谢谢!
答案 0 :(得分:1)
您正在尝试关闭ViewController
而不是表格视图单元格。如果要删除单元格,只需删除数据源中的当前行索引(此处为[String]数组autocomplete
),然后重新加载数据。