2014-11-03 78 views
0

我有一個視圖,其中頂部有一個按鈕,底部有一個自定義單元格的tableview
自定義單元格我有一個文本字段,試圖訪問tableview單元格外的文本字段內容對於頂部按鈕上的行方法,只顯示可見單元格的文本,如果滾動到頂部並點擊保存,底部文本字段值爲零,如果我滾動到底部並點擊保存底部的文本字段值是零,我想它有一些事情與標識符出列可重複單元格。訪問cellforrow之外的tableview內容

//實現代碼如下方法

- (UITableViewCell*) tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{ 
    static NSString *simpleTableIdentifier = @"AcessoryCell"; 

    AcessoryCell *cell = (AcessoryCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
    if (cell == nil) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AcessoryCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 
    } 




for (NSInteger i=0; i<selectedButtonsArray.count; i++) 
{ 

    NSNumber * num = [selectedButtonsArray objectAtIndex:i]; 
    NSIndexPath * indexPath=[NSIndexPath indexPathForRow:[num intValue] inSection:0]; 
    AcessoryCell *cell = (AcessoryCell *) [self.acessoryTableView cellForRowAtIndexPath:indexPath]; 
    NSLog(@"text field text is %@", cell.celltextField.text); 


} 

日誌

text is 1 
text is 1 
text is 1 
text is 1 
text is 1 
text is 1 
text is (null) 
text is (null) 

如果我滾動到底部再登錄是

text is (null) 
text is (null) 
text is 1 
text is 1 
text is 1 
text is 1 
text is 1 
text is 1 

回答

0

是的,在這裏你需要給一個標識符給每個單元格,當您使用static標識符時,它不會加載內存中的每個單元格。

您必須提供一個唯一的標識符來加載UITableView中的所有單元格,並保持原樣滾動UITableView。

替換該行

static NSString *simpleTableIdentifier = @"AcessoryCell"; 

有了這個

NSString *simpleTableIdentifier = @"AcessoryCell"; 

編輯:

- (UITableViewCell*) tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{ 
    NSString *simpleTableIdentifier = @"AcessoryCell"; 

    AcessoryCell *cell = (AcessoryCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
//.... do your stuff 
} 
+0

遺憾的是沒有工作,我得到了同樣的問題。 – 2014-11-03 07:27:07

+0

如果我嘗試訪問單元格通過AcessoryCell * cell =(AcessoryCell *)[tableView dequeueReusableCellWithIdentifier:[self cellIdentifierForIndexPath:indexPath] forIndexPath:indexPath]; 我得到一個錯誤不可見@interface爲「類」聲明選擇器cellIdentifierForIndexPath – 2014-11-03 07:35:10

+0

@SyedIsmailAhamed嘗試刪除靜態關鍵字,檢查編輯的答案 – Kampai 2014-11-03 07:36:33

1

試試這個它的工作:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:YOUR_ROW inSection:YOUR_SECTION]; 

UITableViewCell* cell = [yourTable cellForRowAtIndexPath:indexPath]; 
cell.textLabel.textColor = YOUR_COLOR; 
[yourTable reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
+1

我的表格視圖有一個自定義文本在其中存檔,如果我使用上面的代碼我將無法訪問cel l.cellTextFiled。文本 – 2014-11-03 07:46:56

0

我希望,您正在顯示基於該行的文本字段的不同值。 使用相同的邏輯來獲取文本字段的值。

您是否正在使用基於行的文本字段中顯示值的數組?

+0

最初的文本字段值被硬編碼爲1,然後根據用戶輸入保存文本文件值後,只顯示可見單元格的值。 – 2014-11-03 11:30:44

+0

那麼,您是否使用值爲「1」的初始數組。如果不是,則首先創建一個大小與空行數爲「1」的行數不變的數組。當用戶編輯文本字段(textFieldDidChange)時更新數組值。對你起作用嗎? – 2014-11-04 07:07:34

0

斯威夫特3:

let cell :SampleTableCell = sampleTableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! SampleTableCell