我想知道如何獲取所選uitableviewcell
的textLabel
字符串值。使用iPhone SDK獲取所選單元格的textLabel文本
18
A
回答
48
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// now you can use cell.textLabel.text
}
3
可以使用獲得的細胞[self.tableView的cellForRowAtIndexPath:],然後訪問其textLabel.text屬性,但通常有一種更好的方式。
通常你已經基於你的UITableViewController有權訪問的模型數組填充你的表。因此,在大多數情況下處理此問題的更好方法是獲取所選單元格的行號,並使用該單元格查找模型中的關聯數據。
例如,假設您的控制器具有Buddy
對象,其中有一個name
屬性的數組:
NSArray *buddies;
通過運行一個查詢或東西來填充這個數組。然後在tableView:cellForRowAtIndexPath:
您構建基於每個好友的名稱表視圖單元格:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BuddyCell"];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BuddyCell"] autorelease];
}
cell.textLabel.text = [buddies objectAtIndex:indexPath.row];
return cell;
}
現在,當用戶選擇一排,你只是拉相應的好友對象你的陣列,並用它做什麼。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Buddy *myBuddy = [buddies objectAtIndex:indexPath.row];
NSLog (@"Buddy selected: %@", myBuddy.name);
}
1
if (selected)
{
indicator.image = [UIImage imageNamed:@"IsSelected.png"];
[arrSlectedItem addObject:strselecteditem];
NSLog(@"-- added name is %@", strselecteditem);
}
else
{
indicator.image = [UIImage imageNamed:@"NotSelected.png"];
[arrSlectedItem removeObject:strselecteditem];
NSLog(@"--- remove element is -- %@", strselecteditem);
}
0
如果有人碰到這個蹣跚,並想知道如何在迅速做到這一點的代碼如下。還請記住使用可選綁定來展開可選項,並且還要避免打印出「可選(」Tapped Item Label「)」。
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
// Unwrap that optional
if let label = cell?.textLabel?.text {
println("Tapped \(label)")
}
}
0
我這裏使用的是什麼,很簡單
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){
if editingStyle == UITableViewCellEditingStyle.Delete
{
//create cellobj with indexpath for get it text
let cell = tableView.cellForRowAtIndexPath(indexPath)
let celltext = (cell?.textLabel?.text!)! as String
//do what ever you want with value
print((cell?.textLabel?.text!)! as String)
}
}
相關問題
- 1. 獲取textLabel在單元格中的值Swift 3
- 2. UITableView單元格textLabel顏色
- 3. 保存文本框的值單元格爲textLabel(名稱)SWIFT]
- 4. 更改textlabel單元格不同行中的文本大小uitableview
- 5. 重命名單元格的文本 - iPhone SDK
- 6. 如何從TcxGrid中獲取選定的單元格文本?
- 7. 獲取所選的表格單元格的值
- 8. 從DetailsView獲取單元格文本
- 9. C#單元格LiteralControl獲取文本值
- 10. 無法選擇表格單元格中的文本(iPhone Mobile Safari)
- 11. 使用vba獲取特定文本的單元格列號
- 12. 使用OpenXML SDK將所有單元格值作爲字符串獲取
- 13. 使用jQuery選擇器獲取表格單元格的範圍
- 14. iphone uitableviewcell - 多個顏色在同一個單元格textLabel?
- 15. 選擇單元格條目上的所有Silverlight DataGrid單元格文本
- 16. 如何在iPhone中使用鍵盤獲取文本字段sdk
- 17. DBGrid獲取選定的單元格
- 18. 獲取選定的單元格值
- 19. iPhone SDK - 選擇文本時的選項
- 20. 獲取EditMode中所選DataGridViewRow的特定單元格,與輸入的用於選擇的單元格無關
- 21. 單元格TextLabel多行不再工作
- 22. 在jQuery中獲取所選單元格的值
- 23. 使用有光澤的數據表獲取所選單元格的數據
- 24. 獲取JTable中所有選定行的單元格值
- 25. iPhone SDK - 突出顯示TableView中選定的單元格
- 26. Excel加載項JavaScript - 獲取所選單元格的數據
- 27. 使用jQuery獲取單元格的值
- 28. 如何使用jQuery按索引選取表格單元格的所有元素?
- 29. 如何使用Firefox附加SDK獲取選定的文本?
- 30. 使用jquery獲取錶行中所有單元格的值
那是什麼,我試圖做的,但我只是沒想到FO的NSArray的事情。 – lab12 2010-09-10 15:21:09
很酷。我可以根據你的問題說出這可能是一個更清晰的方式來實現你正在嘗試做的事情。 – cduhn 2010-09-10 15:43:18