如何在构建时创建一个UITableView @属性数组

时间:2013-05-23 14:53:58

标签: ios uitableview

如何使用单个#define编号在构建时控制多个UITableView的数量?

我的应用程序目前需要4个UITableView。之后,这个数字会增加,所以我想在构建时用一个#define来控制这个数量。

但是当我在@property声明中使用数组时出现错误:

#define TOTAL_TX_CHANNELS  4

@interface blah() <UITableViewDataSource, UITableViewDelegate CBPeripheralDelegate>
{
}

@property (strong, nonatomic) UITableView* channel_tableView[ TOTAL_TX_CHANNELS ] ;

诀窍是什么?我应该使用NSArray吗?

4 个答案:

答案 0 :(得分:0)

是的,请使用NSArray

如果在Interface Builder中配置,您可以使用:

@property (weak, nonatomic) IBOutletCollection (UITableView) NSArray * tableViews;      

答案 1 :(得分:0)

为什么你需要控制#UITableViews? 如果你必须这样做,你可以做一些数学运算。

int adder = 4;
int currentNumberOfTvs = 4;

//Do some logic, that will add the number of current table views

if (...) //Test if user has added, or however, if another table view was added, 
{
 adder++;//now adder = 5. So you have a current count of how many table views there are
}

希望有所帮助:)

答案 2 :(得分:0)

我同意其他人的意见,这是一个糟糕的实施。 Wain的答案非常好。

但是,仍然可以通过这种方式实现它,只需确保将属性声明为

@property (assign, nonatomic) UITableView** channel_tableView;

引用计数仅适用于Obj-C对象,但您在此处创建C数组。

请注意,您必须使用malloc来分配存储空间。

如果您不想malloc,请将其作为ivar实施:

@interface Blah {
    UITableView* channel_tableView[4];
}
@end

答案 3 :(得分:0)

解决方案不是使用属性数组,而是在实现范围内使用UITextView数组,.m ...

UITextView * channel_pipe_textview [TOTAL_TX_CHANNELS];

然后,在函数viewDidLayoutSubviews中,我为每个通道创建了一个UITableView,并添加了用于初始化单元格的标准函数,所有这些都遵循Apple优秀的UITableView编程指南。

它工作得很完美,完全独立于故事板:我有四个垂直的并排列,显示有关通道上每个设备的任何类型的图像和文本细节,具有滚动功能,允许每个设备任意数量的设备信道。

Apple UITableView编程指南还提供了教程和示例代码,用于在运行时以编程方式生成UITableView。