2014-04-11 53 views
0

的UITableView上觸摸數據消失時,按照屏幕截圖下面數據從時的UITableView觸摸消失,並且正確釋放

的數據負載時的視圖首次加載,像這樣。

Before touch

在觸摸屏幕,然後釋放,數據消失。 (如果我觸摸並保持,數據仍然存在。)

After touch

我在UITableView的自定義類設置斷點和注意到,當視圖是加載的內(如cellForRowAtIndexPath:)的方法被稱爲,但不是在觸摸之後。從不調用didSelectRowAtIndexPath:方法。

該代碼與DateCell示例非常相似。我試圖加載DatePicker(配置爲僅顯示時間)單元格被觸摸時。

相關代碼如下,以及IB委託和數據源連接的屏幕截圖。如果您需要更多信息,請告訴我。我是iOS新手,因此我會盡可能詳細地瞭解可能的原因和解決方案。

@interface ScheduleTableViewController() 

@property (nonatomic, strong) NSArray *dataArray; 
@property (nonatomic, strong) NSDateFormatter *timeFormatter; 
@property (nonatomic, strong) NSIndexPath *timePickerIndexPath; 
@property (assign) NSInteger pickerCellRowHeight; 
@property (nonatomic, strong) IBOutlet UIDatePicker *pickerView; 
@property (nonatomic, strong) IBOutlet UIBarButtonItem *doneButton; //to be used later for ios 6 compatability 

@end 



@implementation ScheduleTableViewController 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

NSMutableDictionary *itemOne = [[@{ kPeriodKey : @"  Tap a cell to change the survey time: " } mutableCopy ] autorelease]; 
NSMutableDictionary *itemTwo = [[@{ kPeriodKey : @"Morning Survey", 
            kTimeKey : [NSDate date] } mutableCopy] autorelease]; 
NSMutableDictionary *itemThree = [[@{ kPeriodKey : @"Evening Survey", 
            kTimeKey : [NSDate date] } mutableCopy] autorelease]; 
self.dataArray = @[itemOne, itemTwo, itemThree]; 

self.timeFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
[self.timeFormatter setDateFormat:@"h:mm a"]; 
[self.timeFormatter setDateStyle:NSDateFormatterNoStyle]; 
[self.timeFormatter setTimeStyle:NSDateFormatterShortStyle]; 

UITableViewCell *pickerViewCellToCheck = [self.tableView dequeueReusableCellWithIdentifier:kTimePickerID]; 
self.pickerCellRowHeight = pickerViewCellToCheck.frame.size.height; 

[self.tableView setDelegate:self]; 
} 



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

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
if (cell.reuseIdentifier == kDayPeriodAndTimeCellID) { 
    // todo check for ios < 7.0 

    [self displayInlineTimePickerForRowAtIndexPath:indexPath]; 

} else { 

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

} 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell *cell = nil; 

NSString *cellID = kDayPeriodAndTimeCellID; 

if ([self indexPathHasPicker:indexPath]) { 
    cellID = kTimePickerID; 
} 

cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath]; 

if (indexPath.row == 0) { 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
} 

NSInteger modelRow = indexPath.row; 
if (self.timePickerIndexPath != nil && self.timePickerIndexPath.row < indexPath.row) { 
    modelRow--; 
} 

NSDictionary *itemData = self.dataArray[modelRow]; 

if ([cellID isEqualToString:kDayPeriodAndTimeCellID]) { 
    cell.textLabel.text = [itemData valueForKey:kPeriodKey]; 
    cell.detailTextLabel.text = [self.timeFormatter stringFromDate:[itemData valueForKey:kPeriodKey]]; 
} 

return cell; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
if ([self hasInlineTimePicker]) { 
    NSInteger numRows = self.dataArray.count; 
    return ++numRows; 
} 

return self.dataArray.count; 

} 

我加入到現有的應用程序,所以所有現有的UI在XIB文件中實現,但是這個表是在故事板實現。這是委託和數據源插座連接的屏幕截圖。

enter image description here

+0

其中是您的cellForRowAtIndexPath代碼?從我看到的,似乎你正在從自動釋放對象加載表數據而不是保留的對象。 – rocky

+0

剛剛添加'cellForRowAtIndexPath'代碼。我最近在運行分析後添加了autorelease,它報告了項*變量的潛在內存泄漏。在添加autorelease之前,同樣的問題出現了。 – ntaj

+0

如果dequeueReusableCell無法返回單元格,則需要創建單元格 – rocky

回答

0

我認爲你需要在這裏提供一點背景,但我有一個猜測。 UITableView需要一個UITableViewDataSource來告訴它在單元格中應該有哪些數據。你把它勾在XIB上了嗎?如果是這樣,我沒有看到在ScheduleTableViewController中實現的任何方法。

確保你實現了該協議,特別是方法「tableView:cellForRowAtIndexPath:」。這是您實際配置顯示在表格視圖中的單元格的位置。

聽起來像你在InterfaceBuilder中創建了原型表格單元格。對於您創建的每個原型單元格,請確保在屬性檢查器中將其設置爲單元格標識符。然後使用此單元格標識符來標識您在調用「tableView:cellForRowAtIndexPath:」時配置的單元格。

+0

剛添加了'cellForRowAtIndexPath'的代碼。我認爲,由於數據最初正確填充,數據源被正確配置(或我離開基地?)。正確的,原型單元格是在故事板中構建的。我仔細檢查了單元格標識符,並且它們配置正確(界面生成器中的'標識符'文本框與我擁有的兩個原型單元格的代碼中的'標識符'文本框匹配) – ntaj

+0

每個tableview都必須有一個數據源。只是創建原型單元不包括這個要求。原型單元格只是表格準備由數據源配置的單元格。儘管如此,數據源必須在那裏告訴表格要顯示多少行以及如何配置每個單元格。 – devdavid

+0

'dataArray'屬性意味着數據源。我將項目加載到'viewDidLoad'中的數組中,並從'cellForRowAtIndexPath'中將其提取出來。有沒有明確的方式將其配置爲數據源?在任何情況下,不幸的是'cellForRowAtIndexPath'方法在觸摸單元時不會被調用。 – ntaj

0

您是否嘗試過與功能

numberOfSectionsInTableView:tableView 
tableView:tableView numberOfRowsInSection:section 
+0

剛剛發佈了'numberOfRowsInSection'的代碼。 'numberOfSectionsInTableView'默認爲1,等於我有的部分的數量,所以我沒有實現該功能。 – ntaj

0

我有類似的問題(在SWIFT)設置你的表中的數據。它通過將一個實例變量放到我的tableViewManager負責呈現tableView來解決。最初,它的一個實例是通過按鈕操作方法創建和調用的。所以它被ARC解除分配,因此當我試圖與它交互時,tableView消失了。
受啓發︰iOS TableViewController in PopOver disappears after touching tableview