2

我所拥有的是

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

3 回答 3

4

You can set value to the label in the method

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

And can access the cell by the line,

NSIndexPath * path = [NSIndexPath indexPathForRow:0 inSection:1];
        UITableViewCell * cell = [tableView cellForRowAtIndexPath:path];
于 2012-06-07T03:52:28.933 回答
1

No matter what you do with the label in didSelectRow it will always change when you scroll because cellForRow will be called and the labels will be replaced with what you hard-coded

Why don't you put the text and date label strings in an array of dictionaries? for instance

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

Then in your -cellForRow replace

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

with

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

and the same for date

and in -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];
于 2012-06-07T04:04:37.197 回答
0

我的印象是您的 Cell 是 a 的子类UITableViewCell。话虽如此,请尝试以下代码...在此示例中,替换YourCustomCell为您的 subclassed 的名称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];
}
于 2012-06-07T04:05:11.143 回答