【发布时间】:2011-09-12 07:27:06
【问题描述】:
我需要用两列列表显示我的表格视图。我阅读了一些有关通过覆盖 drawRect (here) 来制作一些网格的相关文章。但是,我正在寻找一种简单的方法来设计我的单元格,在笔尖中使用 IB,然后加载它并在每行上推送两个单元格。 drawRect 的示例不合适,因为它涉及手动设置位置。我只需要用一些自动调整大小来推动这两个单元格,就是这样。是否可以?
我正在寻找类似(在 cellForRowAtIndexPath 中):
cell.contentView = emptyUIViewContainer;
[cell.contentView addSubview:FirstColumnUIView];
[cell.contentView addSubview:SecondColumnUIView];
我不需要为这两列设置两个单独的笔尖,因为每一列的格式相同,只是包含一些其他数据。有什么想法吗?
更新:直觉上,我正在尝试做这样的事情:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell1 == nil) {
cell1 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the first cell.
cell1.textLabel.text = ...some text
// Configure the second cell
UITableViewCell *cell2 = [[UITableViewCell alloc] init];
cell2.textLabel.text = ...some text
//set row as container for two cells
UITableViewCell *twoColumnRowView = [[UIView alloc] init]; //initWithFrame:CGRectMake(0, 0, 200, 20)];
cell1.contentView.frame = CGRectMake(0, 0, 100, 20);
[twoColumnRowView addSubview:cell1];
cell2.contentView.frame = CGRectMake(100, 0, 100, 20);
[twoColumnRowView addSubview:cell2];
return twoColumnRowView; // cell;
}
这只是我现在正在玩的一个原始原型。但是代码在运行时崩溃,“由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'-[UIView setTableViewStyle:]: unrecognized selector sent to instance”
更新 2. 我更改了代码以看起来更实用。奇怪,但经过几次尝试后,我让应用程序正常工作,但所有单元格中都有奇怪的黑色背景。代码如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *FirstCellIdentifier = @"FirstCellIdentifier";
static NSString *SecondCellIdentifier = @"SecondCellIdentifier";
// initialize first cell
UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:FirstCellIdentifier];
if (cell1 == nil) {
cell1 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstCellIdentifier] autorelease];
}
//initialize second cell
UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:SecondCellIdentifier];
if (cell2 == nil) {
cell2 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SecondCellIdentifier] autorelease];
}
cell1.textLabel.text = ...data
cell2.textLabel.text = ...data
UITableViewCell *twoColumnRowView = [[UITableViewCell alloc] init];
[twoColumnRowView addSubview:cell1];
//cell2.contentView.frame = CGRectMake(100, 0, 100, 20);
[twoColumnRowView addSubview:cell2];
return twoColumnRowView; // cell;
}
我没有重复使用 twoColumnRowView,但其他两个都可以。
【问题讨论】:
-
每一列都需要是一个独立的单元格吗?您可以将子视图添加到普通单元格并独立填充它们吗?您希望在编辑、选择等方面看到什么样的行为?
-
是的,每一列都应该是独立的单元格项目,我将在排序时使用。作为项目的单元格将包含可通过点击访问的自定义对象(按钮、图像)。问题是我需要一个可旋转的桌子。在纵向模式下,我需要每行显示一列,并且需要在横向模式下显示两列。目前我正在尝试让代码在两个单元格的横向模式下工作。
标签: iphone objective-c uitableview two-columns