的UITableView上觸摸數據消失時,按照屏幕截圖下面數據從時的UITableView觸摸消失,並且正確釋放
的數據負載時的視圖首次加載,像這樣。
在觸摸屏幕,然後釋放,數據消失。 (如果我觸摸並保持,數據仍然存在。)
我在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文件中實現,但是這個表是在故事板實現。這是委託和數據源插座連接的屏幕截圖。
其中是您的cellForRowAtIndexPath代碼?從我看到的,似乎你正在從自動釋放對象加載表數據而不是保留的對象。 – rocky
剛剛添加'cellForRowAtIndexPath'代碼。我最近在運行分析後添加了autorelease,它報告了項*變量的潛在內存泄漏。在添加autorelease之前,同樣的問題出現了。 – ntaj
如果dequeueReusableCell無法返回單元格,則需要創建單元格 – rocky