Iphone 选择UITableView行时是否添加子视图?
我正在开发一个iPhone应用程序,它将XML文件解析为UITableView。它列出了XML文件中项目的所有标题,我试图让单元格展开,并在选择指定单元格时添加所选项目的正文内容 我可以让单元格正确展开,但是当我没有运气将主体内容添加为子视图时。在Iphone 选择UITableView行时是否添加子视图?,iphone,ios,objective-c,uitableview,subview,Iphone,Ios,Objective C,Uitableview,Subview,我正在开发一个iPhone应用程序,它将XML文件解析为UITableView。它列出了XML文件中项目的所有标题,我试图让单元格展开,并在选择指定单元格时添加所选项目的正文内容 我可以让单元格正确展开,但是当我没有运气将主体内容添加为子视图时。在cellForRowAtIndexPath中填充单元格时,我可以添加正文内容,它显示良好 我的问题是,在选定子视图后,如何将其添加到选定单元格中 MycellForRowAtIndexPath函数,注释掉了“功能”主体标签: -(UITableView
cellForRowAtIndexPath
中填充单元格时,我可以添加正文内容,它显示良好
我的问题是,在选定子视图后,如何将其添加到选定单元格中
MycellForRowAtIndexPath
函数,注释掉了“功能”主体标签:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"Cell";
issue *curIssue = [[parser issues] objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
CGRect nameFrame = CGRectMake(0,2,300,15);
UILabel *nameLabel = [[UILabel alloc] initWithFrame:nameFrame];
nameLabel.tag = 1;
nameLabel.font = [UIFont boldSystemFontOfSize:12];
[cell.contentView addSubview:nameLabel];
CGRect bodyFrame = CGRectMake(0,16,300,60);
UILabel *bodyLabel = [[UILabel alloc] initWithFrame:bodyFrame];
bodyLabel.tag = 2;
bodyLabel.numberOfLines = 10;
bodyLabel.font = [UIFont systemFontOfSize:10];
bodyLabel.hidden = YES;
[cell.contentView addSubview:bodyLabel];
}
UILabel *nameLabel = (UILabel *)[cell.contentView viewWithTag:1];
nameLabel.text = [curIssue name];
UILabel *bodyLabel = (UILabel *)[cell.contentView viewWithTag:2];
bodyLabel.text = [curIssue body];
return cell;
}
这是我的heightforrowtaindexpath
函数,我试图在其中添加子视图。尝试执行时,我在尝试分配*bodyLabel
元素时收到EXC_BAD_access异常
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger defCellHeight = 15;
NSInteger heightModifier = 10;
if([self cellIsSelected:indexPath]){
return defCellHeight * heightModifier;
}
return defCellHeight;
}
MyDidSelectRowatineXpath
函数,允许单元格增长/收缩:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BOOL isSeld = ![self cellIsSelected:indexPath];
NSNumber *seldIndex = [NSNumber numberWithBool:isSeld];
[selectedIndexes setObject:seldIndex forKey:indexPath];
UILabel *label = (UILabel *)[tableView viewWithTag:2];
label.hidden = NO;
[curIssView beginUpdates];
[curIssView endUpdates];
}
最后,cellIsSelected
helper函数,如果选择了单元格,则返回true:
-(bool) cellIsSelected:(NSIndexPath *)indexPath
{
NSNumber *selIndex = [selectedIndexes objectForKey:indexPath];
return selIndex == nil ? FALSE : [selIndex boolValue];
}
您可以找到完整的源文件 为什么要在heightForRowAtIndexPath中创建UILabel? 如果要将其添加到行中,请使用上面使用的cellForRowAtIndexPath方法
只需将子视图添加到UITableView单元格或视图中的任何其他位置(使用UIView的addSubview方法,位于didSelectRowAtIndexPath中),即可在选中UITableView行时显示子视图。为什么要在heightForRowAtIndexPath中创建UILabel? 如果要将其添加到行中,请使用上面使用的cellForRowAtIndexPath方法
只需将子视图添加到UITableViewCell或视图中的任何其他位置(使用UIView的addSubview方法,位于didSelectRowAtIndexPath中),即可在选中UITableView行时显示子视图。看起来就像是多次分配和添加同一UILabel。您只需在
cellforrowatinexpath
方法中执行一次。您可能还希望在cellforrowatinexpath
方法中将bodyLabel
设置为隐藏,然后在选择单元格时将其设置为不隐藏。比如:
bodyLabel.hidden = YES;
还有一件事。为什么要取消选择
didSelectRowAtIndexPath
中的行?似乎要多次分配和添加同一UILabel。您只需在cellforrowatinexpath
方法中执行一次。您可能还希望在cellforrowatinexpath
方法中将bodyLabel
设置为隐藏,然后在选择单元格时将其设置为不隐藏。比如:
bodyLabel.hidden = YES;
还有一件事。为什么要取消选择
didselectrowatinexpath
中的行?要在选择时向单元格添加子视图,非常简单:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
/*
.. your other setup
*/
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
[cell.contentView addSubview:<your subview>];
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(nsindepath*)indepath
{
/*
…您的其他设置
*/
UITableViewCell*单元格=[tableView cellForRowAtIndexPath:indexPath];
[cell.contentView addSubview:];
}
要在选择单元时将子视图添加到单元中,非常简单:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
/*
.. your other setup
*/
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
[cell.contentView addSubview:<your subview>];
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(nsindepath*)indepath
{
/*
…您的其他设置
*/
UITableViewCell*单元格=[tableView cellForRowAtIndexPath:indexPath];
[cell.contentView addSubview:];
}
啊,该死。这是我第一次尝试objective-c,所以我对自己正在做的事情都不是100%。大部分都是反复试验。UILabel的隐藏字段很棒!我已将标签/正文字段的创建移回cellforrowatinexpath
方法。然而,我不太清楚在哪里,或者如何将标签设置回unhidden。我想应该是这样的,在didselectrowatinexpath
方法中:UILabel*label=(UILabel*)[tableView view with tag:2];label.hidden=否;但是,这仅显示最后一个单元格的正文,无论选择了哪一个。SetbodyLabel.hidden=NO代码>在中选择行索引路径
。要获取所选行的索引路径,请使用BOOL isSeld=![self-cellIsSelected:indexath.row]
似乎只是缺少了行
。还可以添加到if([self-cellIsSelected:indexPath.row]
Ah,该死。这是我第一次尝试objective-c,所以我对我正在做的事情都不是100%。大部分都是反复试验。UILabel的隐藏字段很棒!我已经将label/body字段的创建移回了CellForRowatineXpath
方法。但是,我不太清楚在哪里,或者如何将标签设置回unhidden.我想应该是这样的,在didselectrowatinexpath
method:UILabel*label=(UILabel*)[tableView view with tag:2];label.hidden=NO;但是,无论选择了哪一个单元格,它都只显示最后一个单元格的正文。在didSelectRowAtIndexPath
中设置bodyLabel.hidden=NO;
。要获取所选行的索引路径,请使用BOOL isSeld=![self cellIsSelected:indexPath.row]
看起来您只是缺少了.row
。如果([self-cellIsSelected:indexPath.row]