I have developed a Collection View app using Objective-C. I am now trying to recreate it using Swift, but I can't seem to translate these last two snippets of code.
Can someone please help me translate my setupCell function in order to configure the Custom Cell?
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as CollectionViewCell
###Translate to Swift
cell setupCell:[self.titles.objectAtIndex(indexPath.row)
return cell
}
class CollectionViewCell: UICollectionViewCell {
###Translate to Swift
-(void)setupCell:(NSDictionary *)dictionary {
NSString *imageFileName = [dictionary valueForKey:#"image"];
self.imageView.image = [UIImage imageNamed:imageFileName];
self.nameLabel.text = [dictionary valueForKey:#"name"];
}
}
Check out this code:
func setUpCell(dictonary:NSDictionary){
var imageFileName : NSString = dictonary.valueForKey("image") as NSString
self.imageView.image = UIImage(named:imageFileName)
self.nameLabel.text = dictonary.valueForKey("name") as NSString
}
let me know if it works for you because I didn't test it
Related
Hi i have an issue in my UICollectionView. First i have a list of images coming from server containing url image.
But when the url is null, the cell is showing random images. I want when the url is null, is just displaying no image.
I'm using SDWebImage for showing the image to UIImageView.
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return listData.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! MyCollectionListViewCell
if let url = self.listData[indexPath.row]["photo"] as? String {
let urlPhoto = NSURL(string:url!)
cell.imageView.sd_setImageWithURL(urlPhoto, placeholderImage:img)
cell.imageView.contentMode = UIViewContentMode.ScaleAspectFill
cell.imageView.layer.masksToBounds = true
cell.imageView.clipsToBounds = true
}
}
As UICollectionViewCell will be reused, just set the image to nil for the cell:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! MyCollectionListViewCell
if let url = self.listData[indexPath.row]["photo"] as? String {
let urlPhoto = NSURL(string:url!)
cell.imageView.sd_setImageWithURL(urlPhoto, placeholderImage:img)
cell.imageView.contentMode = UIViewContentMode.ScaleAspectFill
cell.imageView.layer.masksToBounds = true
cell.imageView.clipsToBounds = true
} else {
cell.imageView.image = nil
}
}
You can also add this method to your cell class :
override func prepareForReuse() {
self.imageView.image = nil
super.prepareForReuse()
}
How do I fix this error?
Variable with getter/setter cannot have an initial value
Here's my code:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell = tableview.dequeueReusableCellWithIdentifier("cell") as UITableViewCell { //Error
cell.textLabel?.text = self.items[indexPath.row] //Error
return cell; //Error
}
}
I think you have an extra set of {} As it is, you're defining a block and assigning it to the cell variable:
var cell: UITableViewCell = tableview.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel?.text = self.items[indexPath.row] //Error
return cell; //Error
And, since dequeueReusableCell... returns a UITableViewCell already, all you really need is:
var cell = tableview.dequeueReusableCellWithIdentifier("cell")
My guess is you cut/copied code that started out looking like:
if let cell = tableview.dequeueReusableCellWithIdentifier("cell") as? MyCellClass {
// some setup code here
return cell
}
I want to Save the First or the Last Image from the UICollectionView and Save it in the UITabelView Cell as UIImage.I try it with the first indexPath if indexPath == 0 catch the UIImage and then save it in CoreData as NSData but it doest works. Have anyone an idea how can i make that or gives another way to Display the UIImage in the UITableViewCell? Thanks for Help.
Here was the code from the UICollectionViewCell:
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("BookPicCell", forIndexPath: indexPath) as! BookPicCell
let cellcore = picture[indexPath.row]
cell.BookImage.image = UIImage(contentsOfFile: cellcore.foto!)
if indexPath.row == 0 {
let firstpic = cell.BookImage.image
let firstcell = UIImagePNGRepresentation(firstpic!)
let firstimag = NSEntityDescription.insertNewObjectForEntityForName("Book", inManagedObjectContext: self.mgdContext) as! Book
firstimag.firstimage = firstcell!
}
do {
try self.mgdContext.save()
} catch {
print("Error")
}
return cell
}
Here was the Code from the UITableViewCell:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let Cell = BookTableView.dequeueReusableCellWithIdentifier("BookCell", forIndexPath: indexPath) as! BookCell
let books = book[indexPath.row]
Cell.BookTitle.text = books.title
let picturecount = books.picture!.count
Cell.BookImageCount.text = "\(picturecount) Picture"
let firstpicture = books.firstimage!
Cell.BookImage.image = UIImage(data: firstpicture)
return Cell
}
I'm trying to follow a tutorial on Core Data in swift, the tutorial seems to be from an older version of swift and i've started running into errors, that the video poster wasn't
The following is written exactly the same, but i'm getting errors:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell {
let CellID: NSString = "Cell"
var cell: UITableViewCell = tableView?.dequeueReusableCellWithIdentifier(CellID) as UITableViewCell
//error: operand of postfix '?' should have optional type: type is uitableview.
if let ip = indexPath {
var data: NSManagedObject = myList[ip.row] as NSManagedObject
cell.textLabel.text = data.valueForKeyPath("username")
//UILabel? does not have a member named "text'
}
return cell
}
I've been stuck on this for an hour and am thinking of giving up. Has swift changed to the point where this can't be done like this
After some trial:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let CellID = "Cell"
var cell = tableView.dequeueReusableCellWithIdentifier(CellID) as! UITableViewCell
var data = myList[indexPath.row] as! NSManagedObject
cell.textLabel?.text = data.valueForKey("username") as? String
return cell
}
Copy and replace your method with the above one,hope the compiler won't complain.
[The code is written in Xcode 6.3(Swift 1.2)]
the main role is to display the playlists names in table view, but it show me word "name" in about 500 cells!
I can make a simple errors, because i only learning swift, and it is my first programming language
There is the code:
import UIKit
import MediaPlayer
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet var tableView: UITableView!
var tableData = MPMediaQuery.playlistsQuery()
var songname: NSString = MPMediaPlaylistPropertyName
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.tableView.reloadData()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.tableData.items.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel.text = songname
return cell
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
println("Row \(indexPath.row) selected")
}
}
Your problem is that you're displaying MPMediaPlaylistPropertyName in the cell which is most likely just the constant string "name"
What you need to do is set the cell's textLabel with the actual name of the playlist which would look something like this in your cellForRowAtIndexPath method
Replace
cell.textLabel.text = songname
With
let playlist: MPMediaPlaylist = tableData.collections[indexPath.row] as MPMediaPlaylist
let playlistName = playlist.valueForProperty(MPMediaPlaylistPropertyName) as NSString
cell.textLabel.text = playlistName;
Also, you need to make sure you're getting the count from the collections of the tableData so in your number of rowsInSectionMethod change:
return self.tableData.items.count
to
return self.tableData.collections.count
For A bit more understanding, what we're doing in the cellForRowAtIndexPath now is:
Getting a specific MPMediaPlaylist item for that particular row
Getting the Name of the playlist and assigning it to a variable
Assigning that name to the cells textLabel
Let me know if you have any other questions
You have to cleanup the cell data since you are re-using the cells,
Right after line
var cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
do
cell.textLabel.text = ""