[英]Issue on TableView Cell
這里我正在開發一個擴展的TableViewCell
,而我正在使用TableView didSelectRowAtIndexPath
方法進行單元擴展,這里我使用了兩個TableView Cell
但我需要將按鈕動作SecondCell擴展。您能幫我如何在這里實現以下代碼,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// disable touch on expanded cell
UITableViewCell *cell = [self.theTableView cellForRowAtIndexPath:indexPath];
if ([[cell reuseIdentifier] isEqualToString:@"ExpandedCellIdentifier"]) {
return;
}
// deselect row
[tableView deselectRowAtIndexPath:indexPath
animated:NO];
// get the actual index path
indexPath = [self actualIndexPathForTappedIndexPath:indexPath];
// save the expanded cell to delete it later
NSIndexPath *theExpandedIndexPath = self.expandedIndexPath;
// same row tapped twice - get rid of the expanded cell
if ([indexPath isEqual:self.expandingIndexPath]) {
self.expandingIndexPath = nil;
self.expandedIndexPath = nil;
}
// add the expanded cell
else {
self.expandingIndexPath = indexPath;
self.expandedIndexPath = [NSIndexPath indexPathForRow:[indexPath row] + 1
inSection:[indexPath section]];
}
[tableView beginUpdates];
if (theExpandedIndexPath) {
[_theTableView deleteRowsAtIndexPaths:@[theExpandedIndexPath]
withRowAnimation:UITableViewRowAnimationNone];
}
if (self.expandedIndexPath) {
[_theTableView insertRowsAtIndexPaths:@[self.expandedIndexPath]
withRowAnimation:UITableViewRowAnimationNone];
}
[tableView endUpdates];
// scroll to the expanded cell
[self.theTableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionMiddle
animated:YES];
}
但是我想在按鈕點擊secondCell時實現擴展。
- (IBAction)expand:(id)sender {
}
您能幫我嗎,謝謝。
這是我的解決方案,您可以通過禁用大小類來創建單獨的.xib文件,從而輕松實現邏輯。 使用布爾變量檢查是否已選擇。 如果是這樣,則重新加載特定的單元格並使用委托方法heightforrow並在此處確定該布爾變量。 很簡單...希望您理解。 如果沒有,請告訴我。 我將分享我制定的代碼。
編輯帖子,下面是我的代碼:-
@implementation ViewController
BOOL sel=NO;
NSMutableArray *a,*b;
NSIndexPath *path;
- (void)viewDidLoad {
[super viewDidLoad];
a=[[NSMutableArray alloc] initWithObjects:@"Apple",@"Mango",@"Orange",@"Pineapple",@"Cricket",@"Bike",@"Music", nil];
b=[[NSMutableArray alloc] initWithArray:a copyItems:YES];
[self.tview registerNib:[UINib nibWithNibName:@"cell1" bundle:nil] forCellReuseIdentifier:@"cell"];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return a.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView beginUpdates];
if([[a objectAtIndex:indexPath.row] isEqualToString:@"Selected"]){
sel=NO;
[a replaceObjectAtIndex:indexPath.row withObject:[b objectAtIndex:indexPath.row]];
}
else{
[a replaceObjectAtIndex:indexPath.row withObject:@"Selected"];
sel=YES;
}
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableView endUpdates];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if([[a objectAtIndex:indexPath.row] isEqualToString:@"Selected"]){
return 100;
}
return 60;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"cell";
cell1 *cell = (cell1 *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// If there is no cell to reuse, create a new one
/* if(cell == nil)
{
cell = [[listcustomcell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}*/
if (!cell)
{
cell = [[cell1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
NSLog(@"%f",[self tableView:self.tview heightForRowAtIndexPath:indexPath]);
cell.btn.frame=CGRectMake(cell.btn.frame.origin.x, [self tableView:self.tview heightForRowAtIndexPath:indexPath]*0.3, cell.btn.frame.size.width*1.3, cell.btn.frame.size.height);
UIButton *b=[[UIButton alloc] initWithFrame:cell.btn.frame];
[cell.btn removeFromSuperview];
[b setTitle:@"button" forState:UIControlStateNormal];
[cell.contentView addSubview:b];
[b addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)checkButtonTapped:(id)sender
{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tview];
NSIndexPath *indexPath = [self.tview indexPathForRowAtPoint:buttonPosition];
if (indexPath != nil)
{
//Just make sure that you've selected no selection option for the tableview.
[self tableView:self.tview didSelectRowAtIndexPath:indexPath];
NSLog(@"You've selected a row %ld",(long)indexPath.row);
}
}
@end
創建一個單獨的.xib並將按鈕放在此處。 要調整按鈕框架的大小,您需要將其刪除並在刷新時再次添加。 希望能幫助到你 :)
我遍歷了您的代碼,我認為您只需要獲取該單元格即可。 請參閱以下代碼以獲取它。
CGPoint touchPoint = [textField convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touchPoint];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
現在,執行與didSelectRowAtIndexPath
相同的步驟。
如果您想擴展/折疊tableView單元格,那么我做了一個演示,可以為您提供所需的確切行為。 這是鏈接: https : //github.com/rushisangani/TableViewCellExpand
請按照演示中的說明設置展開/折疊視圖的約束。
基於內容擁抱和內容壓縮抗性優先級的 。
- (IBAction)expand:(id)sender {
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.theTableView];
NSIndexPath *indexPathInner = [self.theTableView indexPathForRowAtPoint:buttonPosition];
// disable touch on expanded cell
UITableViewCell *cell = [self.theTableView cellForRowAtIndexPath:indexPathInner];
if ([[cell reuseIdentifier] isEqualToString:@"ExpandedCellIdentifier"]) {
return;
}
// deselect row
[tableView deselectRowAtIndexPath:indexPath
animated:NO];
// get the actual index path
indexPath = [self actualIndexPathForTappedIndexPath:indexPath];
// save the expanded cell to delete it later
NSIndexPath *theExpandedIndexPath = self.expandedIndexPath;
// same row tapped twice - get rid of the expanded cell
if ([indexPath isEqual:self.expandingIndexPath]) {
self.expandingIndexPath = nil;
self.expandedIndexPath = nil;
}
// add the expanded cell
else {
self.expandingIndexPath = indexPath;
self.expandedIndexPath = [NSIndexPath indexPathForRow:[indexPath row] + 1
inSection:[indexPath section]];
}
[tableView beginUpdates];
if (theExpandedIndexPath) {
[_theTableView deleteRowsAtIndexPaths:@[theExpandedIndexPath]
withRowAnimation:UITableViewRowAnimationNone];
}
if (self.expandedIndexPath) {
[_theTableView insertRowsAtIndexPaths:@[self.expandedIndexPath]
withRowAnimation:UITableViewRowAnimationNone];
}
[tableView endUpdates];
// scroll to the expanded cell
[self.theTableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionMiddle
animated:YES];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.