2013-05-10 60 views
-2

更新:分配選定單元格的標籤標題屬性

我有它的一些靜態細胞tableview。我想「選定單元格」的textLabel並希望將其分配給NSString財產。

有什麼建議嗎?

+2

你能解釋一下你想要什麼? – Bhavin 2013-05-10 14:04:49

+0

我想選擇的靜態單元格的標題文本分配給屬性 – BluGeni 2013-05-10 14:20:44

+0

我發現正是我一直在尋找最終 http://stackoverflow.com/questions/4444312/how-to-get-title-行選擇在適當的視圖 – BluGeni 2013-05-10 14:26:09

回答

0

答:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    //Now you can use cell.textLabel.text to get the title text of the selected static cell and assign it to your Property. 
    NSString *yourStr = cell.textLabel.text; 
} 
+0

yepp我剛剛發現了這個。非常感謝。 – BluGeni 2013-05-10 14:27:05

0

你並不需要檢查每個indexPath的價值。嘗試是這樣的:

cell.titleLabel.text = [NSString stringWithFormat: @"World %d", indexPath.row]; 
+0

不完全是我正在尋找標題是不同的單詞不只是一個數字的變化。 – BluGeni 2013-05-10 14:16:57

0

1)定義了一個NSArray的財產,

@property(nonatomic, strong) NSArray *titlesArray; 

2)添加標題到陣列,

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    self.titlesArray = [[NSArray alloc] initWithObjects:@"Title 1",@"Title 2",@"Title 3",@"Title 4",@"Title 5",@"Title 6",@"Title 7", nil]; 
} 

3)使用他們這個樣子,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    cell.textLabel.text = [self.titlesArray objectAtIndex:[indexPath row]]; 

    return cell; 
} 
+0

現在,當表格加載時,我有空白單元格。 – BluGeni 2013-05-10 14:16:25

+0

你設置表格視圖以自我的委託和數據源? – 2013-05-10 14:24:29