I have the following problem.
I have set up a search bar in a tableView written in swift. Each tableView cell has a label and an image. The problem is that when I try to search the searchDisplayController displays the right label but the wrong image (it displays the first image from the array).
when the app loads:
when I try to search:
As you can see the images start over from the beginning and the image view doesn't display the correct image for the iPhone cell.
Thanks in advance for your answers.
EDIT1: cellForRowAtIndexPath code:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! CustomCell
var friend : FriendItem
if (tableView == self.searchDisplayController?.searchResultsTableView)
{
friend = self.filteredFriends[indexPath.row]
}
else
{
friend = self.friendsArray[indexPath.row]
}
cell.label1.text = friend.name
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
let item = friendsArray[indexPath.row]
cell.setCell(item.name, image1: item.image)
return cell
}
You are taking item from friendsArray, but you should take if from filteredFriends array when you are searching. Try cell.setCell(friend.name, image1: friend.image)
Related
I have to make a view as image is below. I am confused and not able to darw Screen as Screen will have more Image on Bottom of Image as User will scroll the View and data on the Each time will come up Dynamically and Will on every time View Come up. Thanks any Help Appricated
after a long Research for this kind of view Finally i got a Solution Which is
take a Table View
take multiple Prototype Cell as much you Want
Set the Identifier Individually of each cell
create a Subcalss of UITableViewCell
Method like callForRowatindexPath you have to call like That with Diffrent Cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == 0 {
var cell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as? specificTopicTableViewCell`cell.textlabel.text = "Hello"
return cell!
}
else {
let cell2 = tableView.dequeueReusableCellWithIdentifier(kSecCellIdentifier) as? scondTopicTableViewCell
cell2.textlabel.text = "Hello"
return cell2!
}
}
Using Xcode 7.3 here, wanting to adjust my UITextView margins, but it's not working on all instances when my view loads. Only when scrolling table rows off-screen, then back on, do the UITextViews correct themselves.
I'm using this code in a configureCell function for a custom PostCell:
self.movieSummary.setContentOffset(CGPointMake(5.0, 10.0), animated: false)
...but it only gets applied to the top row's UITextView on first load!?
Here are two screenshots which show the problem with the UITextView highlighted in red...
1) On first load: Only the top row is OK. Other rows are misaligned.
2) After scrolling down and back up: All rows are correctly aligned.
How can I force all the rows/cells/UITextViews to update on first load?
EDIT 1: Here's my code for cellForRowAtIndexPath:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let post = moviePosts[indexPath.row]
if let cell = tableView.dequeueReusableCellWithIdentifier("PostCell") as? PostCell {
cell.request?.cancel()
var posterImg: UIImage?
if let url = post.moviePosterImgPath {
posterImg = FeedVC.imageCache.objectForKey(url) as? UIImage
}
var bgImg: UIImage?
if let url = post.movieBgImgPath {
bgImg = FeedVC.imageCache.objectForKey(url) as? UIImage
}
cell.configureCell(post, posterImg: posterImg, bgImg: bgImg)
return cell
} else {
return PostCell()
}
}
I have a table view like this:
when the user tap one row, I want uncheck the last row and check the selected row. So I wrote my code like this:
(for example my lastselected = 0)
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var lastIndexPath:NSIndexPath = NSIndexPath(forRow: lastSelected, inSection: 0)
var lastCell = self.diceFaceTable.cellForRowAtIndexPath(lastIndexPath) as! TableViewCell
var cell = self.diceFaceTable.cellForRowAtIndexPath(indexPath) as! TableViewCell
lastCell.checkImg.image = UIImage(named: "uncheck")
cell.checkImg.image = UIImage(named: "check")
lastSelected = indexPath.row
}
every thing working fine when I tap a row without scrolling. I realize that when I run the code and scrolling the table immediately and selected the one row. My program will crash with error:
"fatal error: unexpectedly found nil while unwrapping an Optional value"
the error show in this line:
I don't know what wrong in here?
Because you are using reusable cells when you try to select a cell that is not in the screen anymore the app will crash as the cell is no long exist in memory, try this:
if let lastCell = self.diceFaceTable.cellForRowAtIndexPath(lastIndexPath) as! TableViewCell{
lastCell.checkImg.image = UIImage(named: "uncheck")
}
//update the data set from the table view with the change in the icon
//for the old and the new cell
This code will update the check box if the cell is currently in the screen. If it is not currently on the screen when you get the cell to reused (dequeuereusablecellwithidentifier) you should set it properly before display. To do so you will need to update the data set of the table view to contain the change.
Better approach will be storing whole indexPath. not only the row. Try once i think this will work. I had the same problem in one of my app.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var lastCell = self.diceFaceTable.cellForRowAtIndexPath(lastSelectedIndexPath) as! TableViewCell
var cell = self.diceFaceTable.cellForRowAtIndexPath(indexPath) as! TableViewCell
lastCell.checkImg.image = UIImage(named: "uncheck")
cell.checkImg.image = UIImage(named: "check")
lastSelectedIndexPath = indexPath
}
EDIT: or you can try this.
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
var lastCell = self.diceFaceTable.cellForRowAtIndexPath(indexPath) as! TableViewCell
lastCell.checkImg.image = UIImage(named: "uncheck")
}
I am developing a simple table app where I want to display Image in every cell and I have done this and one Image is displayed In all cells :
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
// Create the cell
var cell : UITableViewCell = self.TableViewData.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
cell.textLabel?.text = self.iteams[indexPath.row]
cell.imageView?.image = UIImage(named: "creme_brelee.jpg")
return cell
}
It works fine but I want to display more Images for different cell and for that I have taken on array:
var thumbils : [String] = ["egg_benedict.jpg", "mushroom_risotto.jpg", "full_breakfast.jpg", "hamburger.jpg", "ham_and_egg_sandwich.jpg", "creme_brelee.jpg", "white_chocolate_donut.jpg", "starbucks_coffee.jpg", "vegetable_curry.jpg", "instant_noodle_with_egg.jpg", "noodle_with_bbq_pork.jpg", "japanese_noodle_with_pork.jpg", "green_tea.jpg", "thai_shrimp_cake.jpg", "angry_birds_cake.jpg", "ham_and_cheese_panini.jpg"]
but now I dont know how to display different image for different index in tableView.
I know In objective-C we can do like this:
cell.imageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
But I don't know how to do this in swift.
Please help me for this.
You can use below code to fetch an image from array:
cell.imageView?.image = UIImage(named: thumbils[indexPath.row])
use below code
cell.imageView?.image = UIImage(self.iteams(indexPath.row)
var imagesArray = [UIImage]()
imagesArray.append(UIImage(named: "mypic.png")!)
cell.imageView?.image = imagesArray[indexPath.row]
return cell
I'm trying to make an app for iOS 8, using swift. The goal here is to make a kind of news feed. This feed displays posts from users, which follows a certain pattern.
I thought of using the UITableView where each cell follows a custom layout. The problem appears when I try to access a text label inside it. I try to access it by its tag, but when I do it, the whole app crashes. The error reported is "Swift dynamic cast failed", and I'm using the following code to access the view:
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cellId: String = "cell"
var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellId) as UITableViewCell
if let ip = indexPath{
cell.textLabel.text = myData[ip.row] as String
var lbl = cell.contentView.viewWithTag(0) as UILabel
lbl.text = "ola"
}
return cell
}
Am I doing something wrong? Thanks in advance!
i think the Problem is the Tag 0. All Views are on default value 0. So try another tag value.
Just faced the same issue. The solution was to change tag to 10 and 20. I used 1 and 2 before. Is anybody aware of a range of tags that is used by the system?
So my 'cellForRowAtIndexPath' for a table with an image and a label per row looks like this now:
internal func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier") as? UITableViewCell {
(cell.contentView.viewWithTag(10) as UIImageView).image = IMAGES[indexPath.row]
(cell.contentView.viewWithTag(20) as UILabel).text = LABELS[indexPath.row]
return cell
} else {
NSLog("Prototype did not work")
return UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "errorIdentifier")
}
}
Change the 0 to 1 or another tag, it work for me.
0 is overlapping to another cell label's tag.