<分区>
<分区>
我正在尝试创建一个填充有文本字段的表格单元格。然而,文本字段的数量从来都不是一个固定的数量。我想我只需将数组射入表格单元格,然后从那里完成其余的工作。
但我似乎无法弄清楚如何做到这一点...理想情况下,我将能够从 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
在我的 UITableViewCell
中。然后循环并分配必要数量的文本字段。
我向单元格添加了一个 myArray
属性,然后在上面的方法中执行了一个 alloc init
,但仍然无法从我的 View Controller 访问它。例如cell.myArray = ...
我怎样才能做到这一点?有没有更好的方法我应该这样做?这只是我想到的第一种方式。感谢任何帮助,谢谢你们。
编辑(粗略示例):
//MyCell.h
@interface ITDContactDetailsCell : UITableViewCell
@property (strong, nonatomic) NSMutableArray *myArray;
@end
//MyCell.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_myArray = [[NSMutableArray alloc]init];
//Do some stuff with array (like add UITextViews)
}
}
//MyViewController.m
- (UITableViewCell*)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = @"ContactDetails4LineCell";
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
//This doesn't even register
cell.myArray = [NSMutableArray arrayWithObjects: @"a", @"b", nil];
}
最佳答案
您可以在 ITDContactDetailsCell
类上构建自己的 setter。以下签名是当 VC 说 cell.myArray = ...
- (void)setMyArray:(NSMutableArray *)array {
_myArray = array; // this + what ARC does to it is what the synthesized setter does
// here, we do something with the array so the user can interact with it
// labels are simpler, but textFields are the same idea
CGFloat x = 10, y = 10;
for (NSString *string in array) {
CGRect frame = CGRectMake(x,y,60,20);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.text = string;
[self addSubview:label];
x += 60;
}
}
关于iOS:添加数组以创建动态单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21325451/
相关文章:
ios - UITableViewCell设置突出显示:(BOOL)highlighted is always false
ios - UIColor - getRed : . .. - 引发异常
ios - 自定义 UITableViewCell 不显示标签
ios - 在 Storyboard的标签栏下添加一个 View
iphone - 如何知道 StoreKit In App Purchases 导致了 applicationWillResignActive?
ios - 在 Objective-C 中以编程方式将文本置于图像上