选择行时如何在UITAbleViewCell上添加内容视图? [英] How to add a content view on UITAbleViewCell when a row is selected?
问题描述
我正在尝试在UITableView
中选择某行时添加一个按钮,并且在第二次点击该行时也将其删除,我不希望自定义UITableViewCell
.
任何建议或示例代码将不胜感激.
我尝试过的代码: 在 cellForRowAtIndexPath 方法中
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];
}
和 didSelectRowAtIndexPath 方法
[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
}
I'm trying to add a button when a row is selected in UITableView
and also remove it when row is tapped second time, i don't want custom UITableViewCell
.
Any suggestion or sample code will be appreciated.
code i've tried: in cellForRowAtIndexPath method
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];
}
and in didSelectRowAtIndexPath method
[tableView reloadData];
But still button(textfield with background image) is not rendered.
I think the best way is to use a UITableViewCell subclass. But you can use my answer on another question to make your TableView rerender and place a button in your cell.
- (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
}
这篇关于选择行时如何在UITAbleViewCell上添加内容视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!