我想自定义我的 UITableView 中所有 UITableViewCell 的背景(也许还有边框)。到目前为止,我还没有能够自定义这个东西,所以我有一堆白色的背景单元格,这是默认的。
有没有一种方法可以做到这一点与 iPhone SDK?
您需要将单元格的 contentView 的 backoundColor 设置为您的颜色。如果您使用附件(如公开箭头等) ,他们将显示为白色,所以您可能需要滚动自定义版本的这些。
定制表视图单元格的背景最终成为“全有或全无”的方法。以一种看起来不奇怪的方式更改用于内容单元格背景的颜色或图像是非常困难的。
原因是单元格实际上跨越视图的宽度。圆角只是其绘图风格的一部分,内容视图位于这个区域。
如果您更改内容单元格的颜色,您将在角落处看到白色位。如果您更改整个单元格的颜色,您将拥有一个横跨视图宽度的颜色块。
您当然可以定制一个单元格,但是它并不像您一开始想象的那样简单。
到目前为止,我发现的最好的方法是设置单元格的背景视图和单元格子视图的清晰背景。当然,这看起来不错的表索引样式只,无论有或没有附件。
下面是一个细胞背景为黄色的例子:
UIView *backgroundView = [ [ [ UIView alloc ] initWithFrame:CGRectZero ] autorelease ]; backgroundView.backgroundColor = [ UIColor yellowColor ]; cell.backgroundView = backgroundView; for ( UIView *view in cell.contentView.subviews ) { view.backgroundColor = [ UIColor clearColor ]; }
您会注意到,方法“ textFieldShouldReturn”提供了已经按下 DONE 键的文本字段对象。如果设置了标签,就可以打开该文本字段。或者,您可以跟踪对象的指针并将其与其创建者存储的某个成员值进行比较。
我的自学方法是这样的:
- (BOOL)textFieldShouldReturn:(UITextField *)textField { NSLog(@"%s", __FUNCTION__); bool fDidResign = [textField resignFirstResponder]; NSLog(@"%s: did %resign the keyboard", __FUNCTION__, fDidResign ? @"" : @"not "); return fDidResign; }
与此同时,我还进行了否认辞职的“验证”测试。它只是为了说明,所以如果用户键入 NO!进入战场,它不会撤销。行为是我想要的,但输出的顺序不是我期望的。
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField { NSLog(@"%s", __FUNCTION__); if( [[textField text] isEqualToString:@"NO!"] ) { NSLog(@"%@", textField.text); return NO; } else { return YES; } }
以下是我的 NSLog 输出,其中包含了拒绝和接受。你会注意到我正在返回辞职的结果,但我期望它返回假给我报告给来电者? !除此之外,它还有必要的行为。
13.313 StudyKbd[109:207] -[StudyKbdViewController textFieldShouldReturn:] 13.320 StudyKbd[109:207] -[StudyKbdViewController textFieldShouldEndEditing:] 13.327 StudyKbd[109:207] NO! 13.333 StudyKbd[109:207] -[StudyKbdViewController textFieldShouldReturn:]: did resign the keyboard 59.891 StudyKbd[109:207] -[StudyKbdViewController textFieldShouldReturn:] 59.897 StudyKbd[109:207] -[StudyKbdViewController textFieldShouldEndEditing:] 59.917 StudyKbd[109:207] -[StudyKbdViewController doneEditText]: NO 59.928 StudyKbd[109:207] -[StudyKbdViewController textFieldShouldReturn:]: did resign the keyboard
只要加上
[textField endEditing:YES];
Grigorov 有一些很好的建议-最好的方法是创建一个背景视图,并给它一个颜色,其他的都设置为 clearColor。此外,我认为这种方法是正确清除颜色的唯一方法(尝试他的样品-但设置“ clearColor”而不是“ yellowColor”) ,这就是我正在尝试做的。
其中您要禁用键盘并显示选择器视图。
下面是我遇到的解决这个问题的最有效的方法,使用 willDisplayCell 委托方法(当使用 cell.textLabel.text 和/或 cell.detailTextLabel.text 时,这个方法会处理文本标签背景的白色) :
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { ... }
当调用此委托方法时,通过单元格而不是表视图控制单元格的颜色,如使用:
- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath { ... }
因此,在 cell 委托方法的主体中,将以下代码添加到不同颜色的单元格中,或者仅仅使用函数调用使表中的所有单元格保持相同颜色。
if (indexPath.row % 2) { [cell setBackgroundColor:[UIColor colorWithRed:.8 green:.8 blue:1 alpha:1]]; } else [cell setBackgroundColor:[UIColor clearColor]];
这个方法在我的情况下很有效。
dummy:
IBOutlet UITextField * <nameoftextfieldhere>;
下面是我必须做的工作,让它工作,我认为是必要的任何人与数字键盘(或任何其他没有完成按钮:
我知道这个话题可能很久以前就过时了... ... 但我希望这能帮到某个人,某个地方!
有很多方法可以改进这个表单视图类-附加一个委托,做一些布局,处理当键盘覆盖一个字段时(使用 currentEditingTextField 框架) ,自动启动下一个字段的版本,..。
我个人将它保存在我的框架中,并且总是对它进行子类化以添加特性,这通常是非常有用的“原样”。
希望能有所帮助,干杯
有人在找 Swift 3吗
我同意塞巴,
1)确保你的 UITextField 的代表连接到故事板中的 ViewController
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if ((indexPath.row % 2) == 1) { cell.backgroundColor = UIColorFromRGB(0xEDEDED); cell.textLabel.backgroundColor = UIColorFromRGB(0xEDEDED); cell.selectionStyle = UITableViewCellSelectionStyleGray; } else { cell.backgroundColor = [UIColor whiteColor]; cell.selectionStyle = UITableViewCellSelectionStyleGray; } }
我的解决方案是向 cellForRowAtIndexPath 事件添加以下代码:
UIView *solidColor = [cell viewWithTag:100]; if (!solidColor) { solidColor = [[UIView alloc] initWithFrame:cell.bounds]; solidColor.tag = 100; //Big tag to access the view afterwards [cell addSubview:solidColor]; [cell sendSubviewToBack:solidColor]; [solidColor release]; } solidColor.backgroundColor = [UIColor colorWithRed:254.0/255.0 green:233.0/255.0 blue:233.0/255.0 alpha:1.0];
2)在 ViewController. Swift 文件中实现 UITextField 委托(例如类 ViewController: UIViewController,UITextFieldGenerate {)
在任何情况下都可以工作,甚至使用公开按钮,我认为在 cellForRowAtIndexPath 中处理单元格颜色状态的逻辑比在 cellWillShow 事件中更好。
创建一个视图并将其设置为单元格的背景视图
UIView *lab = [[UIView alloc] initWithFrame:cell.frame]; [lab setBackgroundColor:[UIColor grayColor]]; cell.backgroundView = lab; [lab release];
Swift 3,iOS 9 +
这样可以节省在 willDisplayCell 方法中再次检索数据对象的时间,也可以节省在裁剪/定制表单元格时的两个位置——所有的定制都可以在 cellForRowAtIndexPath 方法中进行。
通过编程将 UITextField 的代表设置为 Swift 3
在 ViewController.Swift 文件中实现 UITextField 委托(例如类 ViewController: UIViewController,UITextFieldGenerate {)
lazy var firstNameTF: UITextField = { let firstname = UITextField() firstname.placeholder = "FirstName" firstname.frame = CGRect(x:38, y: 100, width: 244, height: 30) firstname.textAlignment = .center firstname.borderStyle = UITextBorderStyle.roundedRect firstname.keyboardType = UIKeyboardType.default firstname.delegate = self return firstname }() lazy var lastNameTF: UITextField = { let lastname = UITextField() lastname.placeholder = "LastName" lastname.frame = CGRect(x:38, y: 150, width: 244, height: 30) lastname.textAlignment = .center lastname.borderStyle = UITextBorderStyle.roundedRect lastname.keyboardType = UIKeyboardType.default lastname.delegate = self return lastname }() lazy var emailIdTF: UITextField = { let emailid = UITextField() emailid.placeholder = "EmailId" emailid.frame = CGRect(x:38, y: 200, width: 244, height: 30) emailid.textAlignment = .center emailid.borderStyle = UITextBorderStyle.roundedRect emailid.keyboardType = UIKeyboardType.default emailid.delegate = self return emailid }()
//标记:-处理委托 textField. 。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { view.endEditing(true) } func textFieldShouldReturn(_ textField: UITextField) -> Bool { if textField == firstNameTF { lastNameTF.becomeFirstResponder() } else if textField == lastNameTF { emailIdTF.becomeFirstResponder() } else { view.emailIdTF(true) } return true }
您可以使用 SUM(而不是 COUNT!)和 CASE语句组合,如下所示:
SUM
COUNT
CASE
SELECT SUM(CASE WHEN myColumn=1 THEN 1 ELSE 0 END) FROM AD_CurrentView
由于没有称为 COUNTIF的本机 SQL 函数,COUNTIF()行显然失败了,但是这里的想法是确定 MyColumn 的值为“1”的所有行的百分比。
COUNTIF
COUNTIF()
关于如何在 MSSQL2005环境中正确实现这一点,您有什么想法吗?
注意: 在我自己的测试中,NULL不是一个问题,尽管这可能与环境有关。您可以处理空值,例如:
NULL
SELECT SUM(CASE WHEN ISNULL(myColumn,0)=1 THEN 1 ELSE 0 END) FROM AD_CurrentView
用 Photoshop 或 gimp 创建一个图像作为背景,并将其命名为 myimage。 然后,将这个方法添加到 tableViewController 类中:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { UIImage *cellImage = [UIImage imageNamed:@"myimage.png"];//myimage is a 20x50 px with gradient color created with gimp UIImageView *cellView = [[UIImageView alloc] initWithImage:cellImage]; cellView.contentMode = UIContentViewModeScaleToFill; cell.backgroundView = cellView; //set the background label to clear cell.titleLabel.backgroundColor= [UIColor clearColor]; }
如果您已经将 UITableView 设置为在属性检查器中自定义,那么也可以这样做。
UIView *bg = [[UIView alloc] initWithFrame:cell.frame]; bg.backgroundColor = [UIColor colorWithRed:175.0/255.0 green:220.0/255.0 blue:186.0/255.0 alpha:1]; cell.backgroundView = bg; [bg release];
在尝试了所有不同的解决方案之后,下面的方法是最优雅的一个。 Cell.backgroundView = cellView; 更改下列委托方法中的颜色:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if (...){ cell.backgroundColor = [UIColor blueColor]; } else { cell.backgroundColor = [UIColor whiteColor]; } }
试试这个:
- (void)tableView:(UITableView *)tableView1 willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { [cell setBackgroundColor:[UIColor clearColor]]; tableView1.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"Cream.jpg"]]; }
这将在最新的 Xcode 中工作。
-(UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath { cell.backgroundColor = [UIColor grayColor]; }
如果我们可以重写单元格类,您可以尝试这种方法
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) selectionStyle = .none } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) backgroundColor = selected ? .red : .clear }
我通常按照 Josh 的建议去做,但是我想到了一个稍微有点做作的替代方案,想要和大家分享一下。
您可以利用 COUNT (ColumnName)不计算 NULL 这一事实,并使用如下方法:
SELECT COUNT(NULLIF(0, myColumn)) FROM AD_CurrentView