I am sure the question itself has been asked and answered properly but in Objective C. I am using swift and was wondering how to customize a UITableViewCell properly. I followed this tutorial here http://www.appcoda.com/customize-table-view-cells-for-uitableview/ but I am stuck at properly initializing and using the custom class and XIB file I created. yes, I am a noob. Here is what I have for the standard cell without customization:
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "myCell")
cell.text = postMgr.posts[indexPath.section].title
cell.detailTextLabel.text = postMgr.posts[indexPath.section].description
return cell
}
If someone can translate the Obj C in the tutorial to swift that would be great. Here it is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
cell.thumbnailImageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
cell.prepTimeLabel.text = [prepTime objectAtIndex:indexPath.row];
return cell;
}
Not sure if that's even how it works with iOS7/8. If someone has a better and easier way of customizing the cell, let me know in Swift language.
I appreciate the help already. I am a beginner, please bear with me :)
KM
I just ported the tutorial sample app to Swift
. I'am still using the same CustomTableViewCell written in objective-C (used bridging header to avail the class). My cellForRowIndexPath
looks like
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
var cell:SimpleTableCell! = tableView.dequeueReusableCellWithIdentifier(simpleTableIdentifier) as? SimpleTableCell
if (cell == nil)
{
let nib:Array = NSBundle.mainBundle().loadNibNamed("SimpleTableCell", owner: self, options: nil)
cell = nib[0] as? SimpleTableCell
}
cell.nameLabel.text = tableData[indexPath.row]
cell.thumbnailImageView.image = UIImage(named:thumbnails[indexPath.row])
cell.prepTimeLabel.text = prepTime[indexPath.row];
return cell;
}
find the complete source code here: TableViewApp-Swift
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.