我将 UITextField 子类化以显示一些数据。我想使用相同的数据源——像 UITableView 这样的委托模式。
@interface MySubClass : UITextField <UITextFieldDelegate>
@property (nonatomic, unsafe_unretained) id<MyDataSource> myDatasource;
@property (nonatomic, unsafe_unretained) id<MyDelegate> myDelegate;
-(void) loadStuff;
@end
@implementation MySubClass
-(id) initWithFrame:(CGRect)frame {
...
self.delegate = self; // For the UITextField
...
}
-(void) layoutSubviews {
[self doStuff];
}
-(void) loadStuff {
Data * data = [self.myDatasource ...];
NSString * string = // do stuff to the data for display
self.text = string; //Calls layoutSubviews again, infinite loop.
}
@end
我想过使用 layoutSubviews 从数据源中获取数据,并显示出来。问题是,将文本设置为我刚刚处理的数据,再次触发对 layoutSubviews 的调用。所以你有一个无限循环。
事实上,无论如何更改视图似乎都会触发对 layoutSubviews 的调用。
是否可以复制 UITableView 对其数据源所做的事情?