如何在单击行后更改uilabel的值

时间:2012-06-07 03:48:58

标签: iphone uitableview

我所拥有的是

- (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的值

3 个答案:

答案 0 :(得分:4)

您可以在方法

中为标签设置值
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

可以通过线路访问单元格,

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

答案 1 :(得分: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];

答案 2 :(得分:0)

我认为你的Cell是UITableViewCell的子类。如上所述,请尝试以下代码...在此示例中,将YourCustomCell替换为子类UITableViewCell的名称。

- (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];
}