2012-06-13 28 views
3

我有兩個UILabel爲每一行的TableView。 當在小區中的用戶刷卡,我想減少我的手機第一個標籤的框架,以解決這個問題:editingStyleForRowAtIndexPath同時調用三次。爲什麼?

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

我從比薩瓦萊里奧! 你應該做這樣在你editingStyleForRowAtIndexPath方法:

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

,然後設置幀的單元格。

+0

嗨瓦萊里奧,謝謝你的幫助。我有同樣的問題。 'editStyleForRowAtIndexPath'方法在我以前的控制檯消息中看到的同時被調用三次。所以我連續三次減少幀數。我不期望這一點。爲什麼?我非常感謝你的幫助。 – AlexM

+0

我看到的第一件事:當你分配單元時,你需要使用單元標識符,否則就毫無意義地用「出列」方法從堆棧中獲得一個重用單元。然後爲你的dateformatter使用一個實例變量並設置一次,因爲它需要內存和計算,不適合每個單元格。對於重複出現的問題,您的表中有多少個分區和行? – Valerio

相關問題