Reputation: 11
I have a UITableView Cell in a View Controller serving as a display for autocompletion for a text field. However, selecting one of the autocomplete suggestions does not dismiss the table view cell. I have tried "self.dismissViewControllerAnimated(True, completion: nil)" inside my didSelectRowAtIndexPath function for UITableView. Any thoughts?
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
}
Thanks!
Upvotes: 1
Views: 1466
Reputation: 21
You are trying to dismiss a ViewController
not a table view cell. If you want to remove a cell, just delete current row index in your data source (here to be your [String] array autocomplete
), and do reload data.
Upvotes: 1