Objective c 选择行时,如何在UITAbleViewCell上添加内容视图?
我试图在Objective c 选择行时,如何在UITAbleViewCell上添加内容视图?,objective-c,ios,ios5,uitableview,Objective C,Ios,Ios5,Uitableview,我试图在UITableView中选择一行时添加一个按钮,并在第二次点击该行时将其删除,我不希望自定义UITableViewCell 任何建议或样本代码将不胜感激 我尝试过的代码: 在单元格中,按索引路径方法 if(cell.selected == YES){ UITextField* numOfBottles =[[UITextField alloc] initWithFrame:CGRectMake(240,9.0f,50, 25)];
UITableView
中选择一行时添加一个按钮,并在第二次点击该行时将其删除,我不希望自定义UITableViewCell
任何建议或样本代码将不胜感激
我尝试过的代码:
在单元格中,按索引路径方法
if(cell.selected == YES){
UITextField* numOfBottles =[[UITextField alloc] initWithFrame:CGRectMake(240,9.0f,50, 25)];
numOfBottles.tag = indexPath.row;
[numOfBottles setBorderStyle:UITextBorderStyleNone];
[numOfBottles setBackgroundColor:[UIColor clearColor]];
[numOfBottles setTextColor:[UIColor whiteColor]];
[numOfBottles setTextAlignment:UITextAlignmentLeft];
[numOfBottles setBackground:[UIImage imageNamed:@"blue_dropdown_normal.png"]];
[numOfBottles setFont:[UIFont systemFontOfSize:16.0f]];
[numOfBottles setDelegate:self];
NSString* quantity = [[NSString alloc] initWithString:[subtotalObj.qtyArray objectAtIndex:(indexPath.row - 1)]];
[numOfBottles setText:quantity];
[numOfBottles setTextAlignment:UITextAlignmentCenter];
[numOfBottles setBackgroundColor:[UIColor whiteColor]];
numOfBottles.keyboardType = UIKeyboardTypeDefault;
// numOfBottles.tag = indexPath.row;
[cell.contentView addSubview:numOfBottles];
[numOfBottles release];
}
并在中选择rowatindexpath方法
[tableView reloadData];
但是静止按钮(带有背景图像的文本字段)不会被渲染。我认为最好的方法是使用UITableViewCell子类。 但是你可以使用我的答案来让你的TableView重新播放,并在你的手机里放置一个按钮
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if(indexPath.row == selectedRow){ // if your criteria where met
//add your button
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//do your select things here
selectedRow = indexPath.row; //where selectedRow is a class variable
[self.tableView reloadData]; // rerenders the tableview
}
有一个
UITableView
委托,-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
实现它,在这里您将得到用户点击的行,使用cellforrowatinexpath:
获取当前单元格
,您需要使用部分和行
传递nsindepath
。现在检查cell.contentView子视图
是否有所需的ui按钮
?如果是,请执行removeFromSuperView
elseaddSubView
。如果您的单元格
已经填充了ui按钮
s,您可以给唯一的
标记来区分它们。使用这些代理
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
//Create reference of button with tag 999
cell.contentView addSubView:yourButtonReference]
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
我建议您给我们一个您尝试过的代码片段。我添加了一个变量,这是我认为最快的方法<代码>单元格。选定的
仅适用于UITableViewCell子类
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
UIButton *btnAdded = (UIButton *)[cell viewWithTag:999];
if(btnAdded){
[btnAdded removeFromSuperView];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
}