回调cellForRowAtIndexPath?

回调cellForRowAtIndexPath?

问题描述:

我的项目:回调cellForRowAtIndexPath?

我在我的项目中有一个UITableView。每个UITableViewCell都有一个UITextView作为子视图。当用户点击'添加'按钮,我需要再添加一个UITableViewCellUITextViewUITableView,我必须将焦点设置到添加的UITextView

我试了一下:

一旦添加按钮的用户点击,我会更新的UITableViewnumberOfRowsInSection,随后我会打电话给[UITableView reloadData]调用[UITableView cellForRowAtIndexPath]。在这里,我将追加UITextView作为UITableViewCell的子视图。在此之前它工作正常。

我的问题:

我需要将焦点设置到UITextView一旦[UITableView cellForRowAtIndexPath]被调用后。当我在[UITableView reloadData]之后调用UITextView的设置焦点方法时,它不起作用。我想知道,[UITableView cellForRowAtIndexPath]有没有回调方法?

感谢您的回答。

+2

如果使用'[UITableView insertRowsAtIndexPaths:withRowAnimation:]'(仅用于新行),会发生什么? – *foe

+0

他的问题不是关于添加新行@*foe,而是询问如何在添加新行后将uitextview设置为firstresponder。 – Pavan

+0

kiru,你使用自定义单元格吗? – Pavan

试试这个,

在添加按钮动作

//Assuming only 1 section, if you have more section find sections by `numberOfSections` method 
NSInteger totalRows = [self.tableView numberOfRowsInSection:0]; 
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:totalRows inSection:0]; 

[tableView beginUpdates]; 
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; 
[tableView endUpdates]; 

//If you have custom cell 
CustomCell *cell = (CustomCell *)[self.tableView cellForRowAtIndexPath:indexPath]; 
[cell.textView becomeFirstResponder]; 

//If you are not using custom cell, then set tag to textView and 
//UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
//UITextView *textView = (UITextView *)[cell viewWithTag:kTextViewTag]; 
//[textView becomeFirstResponder]; 
+0

感谢您的回答 – Kirubachari

你叫你的reloadData方法,你可以做这样的事情后: 要到最后一节的最后一排的参考...

// First figure out how many sections there are 
NSInteger lastSectionIndex = [self.myTableView numberOfSections] - 1; 

// Then grab the number of rows in the last section 
NSInteger lastRowIndex = [self.myTableView numberOfRowsInSection:lastSectionIndex] - 1; 

// Now just construct the index path 
NSIndexPath *pathToLastRow = [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex]; 

//Grab a reference of your cell from the last row 
MyCustomCell *myCC = [self.myTableView cellForRowAtIndexPath:pathToLastRow]; 

//Use this cell reference to set focus to your UITextView 
[myCC.myTextView setFirstResponder:YES]; 
+0

调用'reloadData'和实际出现在表视图中的行之间的不可避免的延迟是什么?构建一个到*存在*行的索引路径可能会破坏表视图... – *foe

+0

直到reloadData方法完成其所有代码的执行,包括显示最后一行细胞。在另一个说明中,我会这样做的方法是通过正确的方法,使用'insertionRowAtIndexPath'方法添加一个新行,从此之后抓取最后一个单元格,然后为该textview设置焦点。 – Pavan

+0

所以你确定'reloadData'是同步的吗? – *foe