如何在选中某一行时在UITAbleViewCell上添加内容视图?
我试图添加一个button,当在UITableView
select一个行,也删除它时,第二次点击行,我不想自定义UITableViewCell
。
任何build议或示例代码将不胜感激。
代码我试过了:在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];
但仍然button(带背景图像的文本字段)不呈现。
我认为最好的方法是使用UITableViewCell子类。 但是,你可以在另一个问题上使用我的答案,使您的TableView重新渲染,并在您的单元格中放置一个button。
- (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
实现它,你会得到哪一行用户点击,获取当前cell
使用cellForRowAtIndexPath:
您需要传递NSIndexPath
使用section
和row
。 现在检查cell.contentView subviews
是否有你想要的UIButton
? 如果是,则执行removeFromSuperView
else addSubView
。 如果你的cell
已经填充了UIButton
你可以给出unique
标签来区分它们。
使用这些代表
- (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];
}
- (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]; } }