2015-01-26 77 views
-1

我有一個tableviewcell與5個不同的單元格。每個視圖都有一個標籤,其中包含以下內容之一。 1個文本框,1個文本,視圖1,開關或1個滑塊。我正在嘗試在索引路徑中查找行的tableviewcell,以便我可以檢索用戶輸入的信息。以下是我對單元和不同開關的代碼。如果可能,我只需要知道如何實現這一點。謝謝(頁面)我在考慮只是標記元素,而不是爲行尋找單元格,這就是爲什麼您會看到標記代碼。如何獲得從數組填充的tableviewcell的索引路徑?

-(UITableViewCell *)listCell:(UITableView *)tableView IndexPath:(NSIndexPath*)indexPath 
{ 
static NSString *textFieldIdentifier = @"textFieldCell"; 
static NSString *textViewIdentifier = @"textViewCell"; 
static NSString *switchIdentifier = @"switchCell"; 
static NSString *seekBarIdentifier = @"seekBarCell"; 
static NSString *datePickerIdentifier = @"datePickerCell"; 

//DBSectionDetailsTableViewCell *cell = (DBSectionDetailsTableViewCell *) [tableView dequeueReusableCellWithIdentifier:textViewIdentifier]; 
UITableViewCell *cell6 = [tableView dequeueReusableCellWithIdentifier:datePickerIdentifier]; 


results = [listArray objectAtIndex:[indexPath row]]; 


if([indexPath row] < listArray.count) 
{ 
    if (([[results objectAtIndex:2]isEqualToString:@"EditText"])) 
    { 

     DBSectionDetailsTableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:textFieldIdentifier forIndexPath:indexPath]; 
     cell2.titleLabel.text = [results objectAtIndex:1]; 
     //title button 
     cell2.textField.tag = [indexPath row]; 
     cell2.textField.delegate = self; 

     [cell2.textField addTarget:self action:@selector(textFieldDidEndEditing:) forControlEvents:UIControlEventTouchUpInside]; 


    return cell2; 
    } 

    else if (([[results objectAtIndex:2]isEqualToString:@"EditTextLarge"])) 
    { 
     TextViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:textViewIdentifier forIndexPath:indexPath]; 

     // cell = textFieldCell; 
     cell.titleLabel.text = [results objectAtIndex:1]; 
     cell.textView.tag = [indexPath row]; 
     cell.textView.delegate = self; 
     // [cell.textView addTarget:self action:@selector(textViewDidEndEditing:) forControlEvents:UIControlEventTouchUpInside]; 


     return cell; 
    } 

    else if (([[results objectAtIndex:2]isEqualToString:@"Switch"])) 
    { 

     SwitchTableViewCell *cell3 = [tableView dequeueReusableCellWithIdentifier:switchIdentifier forIndexPath:indexPath]; 


     // cell3 = switchCell; 
     cell3.titleLabel.text = [results objectAtIndex:1]; 
     cell3.switchOutlet.tag = [indexPath row]; 


     return cell3; 
    } 

    else if (([[results objectAtIndex:2]isEqualToString:@"SeekBar"])) 
    { 

     SeekBarTableViewCell *cell4 = [tableView dequeueReusableCellWithIdentifier:seekBarIdentifier forIndexPath:indexPath]; 


     //cell4 = seekBarCell; 
     cell4.titleLabel.text = [results objectAtIndex:1]; 
     cell4.slider.tag = [indexPath row]; 

     return cell4; 
    } 

    else if (([[results objectAtIndex:2]isEqualToString:@"DatePicker"])) 
    { 

     DatePickerTableViewCell *cell5 = [tableView dequeueReusableCellWithIdentifier:datePickerIdentifier forIndexPath:indexPath]; 

     //cell5 = datePickerCell; 
     cell5.titleLabel.text = [results objectAtIndex:1]; 
     cell5.datePicker.tag = [indexPath row]; 
     cell5.datePicker.delegate = self; 

     [cell5.datePicker addTarget:self action:@selector(datePickerDidEndEditing:) forControlEvents:UIControlEventTouchUpInside]; 

     return cell5; 
    } 


} 

//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 


return cell6; 

}

+0

此外,tableview是動態的,當然是因爲tableviewcells正在由消費者在後端設置的任何東西填充。 – TheChef 2015-01-26 20:53:45

+0

非常混亂。如果你有單元並想知道索引路徑,可以使用'indexPathForCell'。但是這聽起來有一個數組索引,並希望將其與索引路徑相關聯 - 這是數據源對象的責任。 – 2015-01-26 21:01:35

+0

是的,理解...我試圖使用這樣的東西 NSIndexPath * newIndex = [NSIndexPath indexPathForRow:0 inSection:0]; DBSectionDetailsTableViewCell * cell2 =(DBSectionDetailsTableViewCell *)[li​​stTable cellForRowAtIndexPath:newIndex]; 在我的「保存」功能,但我仍然返回「零/無」的一切。我認爲去按鈕標記的路線可能會更好,我只是想看看我是否可以使用NSIndex來實現我想要的,而不用做所有的標記和附加的函數調用等。 – TheChef 2015-01-26 21:19:02

回答

1

可以使用[tableView cellForRowAtIndexPath:indexPath]獲取從表中可見單元格。屏幕外的單元格將從UITableView中刪除,您將無法訪問它們,因爲它們將被放回到可重用單元隊列中。


注1:你不應該依靠你UITableView存儲當前的數據狀態。它拋棄了在屏幕外滾動的單元格,因此如果您不將數據保存在其他地方,它將永遠丟失。


注2:此代碼是不是特別安全:

results = [listArray objectAtIndex:[indexPath row]]; 
if([indexPath row] < listArray.count) // This is always true. 
             // If it isn't true, the above 
             // objectAtIndex: will crash your app. 
+0

謝謝,它會一直是真的,因爲總是會有後端數據...「列表數組」,除非它被黑掉,所有的數據被刪除,如果發生那麼好......整個應用程序將無法工作因爲它基於數據庫。 但是,非常感謝 – TheChef 2015-01-26 21:08:13

+0

如果它總是真的,爲什麼麻煩檢查呢? – 2015-01-26 21:15:05

+0

我想我現在可以把它拿出來,但是在開始時,這部分應用程序的後端還沒有完成。所以,而不是刪除它,我只是把它切換。在完成一個過程結束時,我清理了我的代碼,但是這個過程並不完整,所以我仍然有註釋行等被去掉。但再次感謝您的額外的眼睛。我想我會標記我試圖檢索信息的項目。我只是試圖避免與之相關的額外功能。 – TheChef 2015-01-26 21:27:27

0

在用戶輸入其每個單元應該保存的信息,因爲你無法到達,一旦細胞離開視圖以後再使用。