2012-06-07 13 views
2

什麼我有是如何單擊行後更改的UILabel的價值

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
     cell = customCell; 
     self.customCell = nil; 
    } 

    // Configure the cell. 
    switch (indexPath.row) { 
     case 0: 
      cell.title.text = @"iPhone!"; 
      cell.date.text = @"December 25, 2009"; 
      cell.imageView.image = [UIImage imageNamed:@"iphone.png"]; 
      break; 
     case 1: 
      cell.title.text = @"Second Cell"; 
      cell.date.text = @"December 26, 2009"; 
      //Put in your own image. Make sure it is 120 by 100 
      cell.imageView.image = [UIImage imageNamed:@""]; 
      break; 
     default: 
      break; 
    } 
    return cell; 
} 




// Override to support row selection in the table view. 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSLog(@"index %d",indexPath.row); 
    NSIndexPath *index = [NSIndexPath indexPathForRow:indexPath.row inSection: 0]; 
    NSArray *array = [NSArray arrayWithObject: index]; 
    cell.title.text = @"tetete"; 
    [self.myTable reloadRowsAtIndexPaths: array withRowAnimation: UITableViewRowAnimationFade]; 
} 

,如果你點擊一排我的目的是,你會改變該行的標籤的文本。

然而,reloadRowsAtIndexPath將調用cellForRowAtIndex而這一次它將舊的文本分配到的UILabel

我的問題:我應該怎麼改變,這樣我可以在該行點擊後更改的UILabel的價值

回答

4

您可以在方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

設定值的標籤,並且可以通過線接入小區,

NSIndexPath * path = [NSIndexPath indexPathForRow:0 inSection:1]; 
     UITableViewCell * cell = [tableView cellForRowAtIndexPath:path]; 
1

不管你在didSelectRow標籤做什麼,當你滾動因爲cellForRow將被調用它會改變的,而唱片公司會與你硬編碼什麼

你爲什麼不把文本替換並在字典數組中標註日期標籤字符串? 例如

NSMutableArray *array = [[NSMutableArray alloc] init]; 
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 
[dict setValue:@"iPhone!" forKey:@"title"]; 
[dict setValue:@"December 25th, 2009" forKey:@"date"]; 
[array addObject:dict]; 
[dict release]; 
dict = [[NSMutableDictionary alloc] init]; 
[dict setValue:@"Second Cell!" forKey:@"title"]; 
[dict setValue:@"December 26th, 2009" forKey:@"date"]; 
[array addObject:dict]; 
[dict release]; 

然後在你的-cellForRow更換

cell.title.text = @"iPhone!"; 

cell.title.text = [[array objectAtIndex:indexPath.row] objectForKey:@"title"]; 

與同爲日

-didSelectRow

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 
[dict setValue:@"test!" forKey:@"title"]; 
[dict setValue:@"December 27th, 2009" forKey:@"date"]; 
[array replaceObjectAtIndex:indexPath.row withObject:dict]; 
[dict release]; 
0

我覺得你的Cell是UITableViewCell的子類。用這個說法,試試下面的代碼...在這個例子中,用你的子類UITableViewCell的名稱替換YourCustomCell

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
NSLog(@"index %d",indexPath.row); 
NSIndexPath *index = [NSIndexPath indexPathForRow:indexPath.row inSection: 0]; 
NSArray *array = [NSArray arrayWithObject: index]; 

// This is where you make the pointer to your cell. 
YourCustomCell *cell = (YourCustomCell*)[tableView cellForRowAtIndexPath:index]; 

cell.title.text = @"tetete"; 
[self.myTable reloadRowsAtIndexPaths: array withRowAnimation: UITableViewRowAnimationFade]; 
}