editingStyleForRowAtIndexPath同时调用三次。为什么?

时间:2012-06-13 20:33:49

标签: iphone ios uitableview uilabel

我有一个TableView,每行有两个UILabel。 当用户在单元格上滑动时,我想减少单元格上第一个标签的框架,以解决此问题:

bug_image

这是我的代码:

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:NO] autorelease];
    }

    custom_cell = [[[UILabel alloc] initWithFrame:CGRectMake(10, 0, 200, 45)] autorelease];


    custom_cell.textColor = [UIColor blackColor];

    custom_cell.backgroundColor = [UIColor clearColor];

    [custom_cell setText:[[self.Notes objectAtIndex:indexPath.row ]objectForKey:@"Text"]];

    [cell.contentView addSubview:custom_cell];

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat: @"dd MMM yy"];

    [dateFormat setLocale:[NSLocale currentLocale]];

    NSDate *dateTmp = [[self.Notes objectAtIndex:indexPath.row] objectForKey:@"CDate"];

    cell.detailTextLabel.text = [dateFormat stringFromDate: dateTmp];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    [dateFormat release];

    return cell;


}

然后,为了减少我单元格上第一个标签的框架,我写了这段代码:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"row=%d", indexPath.row);

    custom_cell.frame = CGRectMake(10, 0, 150, 45);

    return UITableViewCellEditingStyleDelete;
}

结果部分正确。我的tableView的最后一个标签总是减少,而不是正确的。

控制台也打印三次NSLog消息。为什么呢?

2012-06-13 22:07:34.809 myVoyager[1935:16103] row=0
2012-06-13 22:07:34.810 myVoyager[1935:16103] row=0
2012-06-13 22:07:34.813 myVoyager[1935:16103] row=0

谢谢, 来自比萨的亚历山德罗(对不起我的英语!)

1 个答案:

答案 0 :(得分:1)

我是来自比萨的Valerio! 你应该在editStyleForRowAtIndexPath方法中这样做:

UITableViewCell *myCell = [self.tableView cellForRowAtIndexPath:indexPath];

然后将帧设置为单元格。