我有经典的 TableView,如果您滑动并单击按钮,您可以删除项目。我知道如何在单元格上设置自定义背景,但我找不到如何为其设置自定义字体和颜色。
谢谢你的帮助!
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
var deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default,
title: "Delete",
handler: {
(action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
println("Delete button clicked!")
})
deleteAction.backgroundColor = UIColor.redColor()
return [deleteAction]
}
嗯,我发现设置自定义字体的唯一方法是使用appearanceWhenContainedIn
的方法UIAppearance
协议。此方法在 Swift 中尚不可用,因此您必须在 Objective-C 中执行此操作。
我在实用程序 Objective-C 类中创建了一个类方法来设置它:
+ (void)setUpDeleteRowActionStyleForUserCell {
UIFont *font = [UIFont fontWithName:@"AvenirNext-Regular" size:19];
NSDictionary *attributes = @{NSFontAttributeName: font,
NSForegroundColorAttributeName: [UIColor whiteColor]};
NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString: @"DELETE"
attributes: attributes];
/*
* We include UIView in the containment hierarchy because there is another button in UserCell that is a direct descendant of UserCell that we don't want this to affect.
*/
[[UIButton appearanceWhenContainedIn:[UIView class], [UserCell class], nil] setAttributedTitle: attributedTitle
forState: UIControlStateNormal];
}
这可行,但绝对不理想。如果您不将 UIView 包含在包含层次结构中,那么它最终也会影响公开指示器(我什至没有意识到公开指示器是 UIButton 子类)。另外,如果您的单元格中有一个 UIButton,该 UIButton 位于单元格的子视图内,那么该按钮也会受到此解决方案的影响。
考虑到复杂性,最好仅使用可定制性更强的开源库之一来实现表格单元格滑动选项。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)