如何更改UITableView
中项目的字体类型?我一直试图这样做,但我无法改变它。我是编程新手,所以请尽可能以最简单的方式解释。
这是整个代码
**ViewController 1**
import UIKit
import Foundation
var items:[String] = []
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
cell.textLabel?.text = items[indexPath.row]
return cell
}
override func viewWillAppear(animated: Bool) {
if var storeditems: AnyObject? = NSUserDefaults.standardUserDefaults().objectForKey("items") {
items = []
for var i = 0; i<storeditems?.count; ++i {
items.append(storeditems?[i] as NSString)
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
items.removeAtIndex(indexPath.row)
NSUserDefaults.standardUserDefaults().setObject(items, forKey: "items")
NSUserDefaults.standardUserDefaults().synchronize()
}
}
}
}
}
**View Controller 2**
import UIKit
class ViewController2: UIViewController, UITextFieldDelegate {
@IBOutlet weak var textField: UITextField!
@IBAction func button(sender: AnyObject) {
items.append(textField.text)
NSUserDefaults.standardUserDefaults().setObject(items, forKey: "items")
NSUserDefaults.standardUserDefaults().synchronize()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
答案 0 :(得分:0)
请使用以下代码设置tableview单元格字体以及背景颜色。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
cell.textLabel.text = [yourAry objectAtIndex:(indexPath.row)];
cell.textLabel.font = [UIFont fontWithName:@"Avenir Next" size:16]; // set font style
cell.contentView.backgroundColor = [UIColor colorWithRed:239.0/255.0 green:239.0/255.0 blue:239.0/255.0 alpha:1.0]; // set tableview cell background color.
return cell;
}
快速语言
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel.text = String(yourAry[indexPath.row] as NSString)
cell.textLabel.font = UIFont (name: "Avenir Next", size: 16) // set font style
return cell