将 UITextField 与 UITextView 相同时如何避免编译器警告
场景:
我有两个 UITableViewCells 分别包含 UITextField 属性和 UITextView 属性。显然,这两个属性都有共同的属性,例如“text”和“textColor”
目标:
不加区别地将字符串分配给 UITextField/UITextView 的 text 属性,而不引发编译器警告。能做到吗?
这是我正在做的一个例子:
目前我这样做是为了避免编译器警告:
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UITextField *textField;
UITextView *textView;
switch (indexPath.section) {
case SectionDescription:
textField = (UITextField *)[(TextFieldCell *)cell contents];
textField.text = self.plan.description;
break;
case SectionNotes:
textView = (UITextView *)[(TextViewCell *)cell contents];
textView.text = self.plan.notes;
break;
}
但我想做一些更接近于此的事情,而没有编译器警告:
NSString *contents = [self getContents];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UIView *textView = (?? *)[(?? *)cell contents];
textView.text = contents;
有没有标准的方法来做到这一点?我知道如果两个对象都符合通用协议,我可以执行类似“UIView *textView = [单元格内容];”的操作这个问题有答案吗?
Scenario:
I have two UITableViewCells containing a UITextField property and UITextView property respectively. Obviously both of these properties have properties in common, such as 'text' and 'textColor'
Objective:
Indiscriminately assign a string to the UITextField/UITextView's text property without raising compiler warnings. Can it be done?
Here's an example of what I'm doing:
Currently I do this to avoid compiler warnings:
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UITextField *textField;
UITextView *textView;
switch (indexPath.section) {
case SectionDescription:
textField = (UITextField *)[(TextFieldCell *)cell contents];
textField.text = self.plan.description;
break;
case SectionNotes:
textView = (UITextView *)[(TextViewCell *)cell contents];
textView.text = self.plan.notes;
break;
}
But I'd like to do something closer to this without the compiler warnings:
NSString *contents = [self getContents];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UIView *textView = (?? *)[(?? *)cell contents];
textView.text = contents;
Is there a standard way to do this? I know if both objects conformed to a common protocol I could do something like "UIView *textView = [cell contents];" Is there an answer to this question?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用
[(id)textView setText:contents]
。Try using
[(id)textView setText:contents]
.