![](/img/trans.png)
[英]How to Set UITableViewCellStyleSubtitle if use “dequeueReusableCellWithIdentifier:forIndexPath:” method
[英]how to use the function dequeueReusableCellWithIdentifier:forIndexPath?
我知道dequeueReusableCellWithIdentifier:forIndexPath是由tableviewcontroller中的tableView方法調用的。 如果我理解正確,將多次調用tableView方法,直到填充所有單元格為止。 但是我不知道您從哪里獲得參數IndexPath的值? 我想對我創建的方法使用dequeueReusableCellWithIdentifier:forIndexPath,因為我想訪問單元格以復制其屬性的某些值。
注意:我已經填充了單元格,這意味着我成功使用了tableView方法。
(編輯)其他信息:我正在嘗試創建一個配置文件並編輯配置文件表視圖。 在個人資料表視圖中,我顯示了用戶的姓名,地址,聯系方式等。 另外,我有一個名為“編輯配置文件”的文件。 在編輯配置文件中,我具有每個類別的文本字段(名稱,地址等)。 我想做的是,如果我編輯文本字段的內容,則應該能夠在個人資料表格視圖中顯示新內容。 一個例子是:在配置文件視圖中,我顯示->名稱:人類,地址:地球(每個都在其自己的單元格中)。 現在,如果我進入editprofile tableview,我將編輯內容-> name:alien,address:mars。 之后,有一個名為“應用”的按鈕,用於結束內容的編輯並返回到配置文件表視圖。 如果我返回到個人資料視圖,現在的顯示應該是name:alien,address:mars,而不是name:human,address:earth。
如果有幫助,這里有一些代碼。 該代碼由tableviewcontroller中的按鈕調用。 “ MyCell”是我的單元格的類。 此代碼無法正常工作。 我希望有人可以幫助我解決此問題。
- (IBAction)updateCopies:(id)sender {
static NSString *ident = @"MyCell";
NSIndexPath *indexPath;
//create cell
MyCell *cell = [self.tableView dequeueReusableCellWithIdentifier:ident forIndexPath:indexPath];
//create variable for accessing cells
int row = [indexPath row];
_labelValues[row] = cell.textField.text
}
僅在需要為表視圖提供要顯示的單元格時,才應使用dequeueReusableCellWithIdentifier
。 如果要在特定索引處獲取UITableViewCell對象,則應使用cellForRowAtIndexPath 。
你的問題
您真正需要的是模型類。 然后,您可以將其傳遞給編輯控制器,后者將更改屬性。 然后,當您返回tableView時,可以重新加載它並顯示新屬性。
您還可以做的是為您的編輯配置文件控制器創建一個委托協議,類似於EditProfileViewControllerDelegate
的東西:
protocol EditProfileViewControllerDelegate {
- (void)editProfileViewController:(EditProfileViewController *)controller didUpdateName:(NSString *)name address:(NSString *)address;
}
您可以在表視圖控制器中實現此委托,並在更改文本時使用它來更新值。 但是,這很快變得難以處理,我不建議您使用適當的模型類來推薦它。
用這個
- (IBAction)updateCopies:(id)sender {
MyCell *parentCell = (MyCell *)sender.superview;
while (![parentCell isKindOfClass:[MyCell class]]) { // iOS 7 onwards the table cell hierachy has changed.
parentCell = parentCell.superview;
}
UIView *parentView = parentCell.superview;
while (![parentView isKindOfClass:[UITableView class]]) { // iOS 7 onwards the table cell hierachy has changed.
parentView = parentView.superview;
}
UITableView *tableView = (UITableView *)parentView;
NSIndexPath *indexPath = [tableView indexPathForCell:(MyCell *)parentCell];
NSLog(@"indexPath = %@", indexPath);
}
您可以使用CGPoint獲得indexPath。您可以使用dequeueResusableCell來實現單元的可重用性。
- (IBAction)updateCopies:(id)sender {
CGPoint position = [sender convertPoint:CGPointZero
toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:position];
//create variable for accessing cells
MyCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
int row = [indexPath row];
_labelValues[row] = cell.textField.text
}
希望對您有幫助。
好吧,我得到了您想要完成的工作。
首先,當您單擊/選擇一個單元格並轉到“編輯配置文件”頁面時,會調用一個delegate
。 那位代表是
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
///
}
創建一個全局變量,例如selectedIndexPath
,該變量保存正在編輯的當前單元格索引路徑。 每次進入編輯個人資料頁面時,請更新此值。
像這樣
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selectedIndexPath = indexPath;
// code to go to edit page...
}
現在在您的updateCopies
方法中,像這樣
- (IBAction)updateCopies:(id)sender {
//get the existing cell with the indexPath
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[selectedIndexPath]];
[self.tableView endUpdates];
//rest of your code goes here...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.