我必須在這裏錯過簡單的東西,但它逃脫了我。在用戶將新人員輸入到可變數組後,我想更新表格。可變數組是數據源。我相信我的問題在cellForRowAtIndexPath
之內。Tableview在添加人後沒有正確更新
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TextFieldCell *customCell = (TextFieldCell *)[tableView dequeueReusableCellWithIdentifier:@"TextCellID"];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (indexPath.row == 0) {
if (customCell == nil) {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"TextFieldCell" owner:nil options:nil];
for (id currentObject in nibObjects) {
if ([currentObject isKindOfClass:[TextFieldCell class]])
customCell = (TextFieldCell *)currentObject;
}
}
customCell.nameTextField.delegate = self;
cell = customCell;
}
else {
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
cell.textLabel.text = [[self.peopleArray objectAtIndex:indexPath.row-1] name];
NSLog(@"PERSON AT ROW %d = %@", indexPath.row-1, [[self.peopleArray objectAtIndex:indexPath.row-1] name]);
NSLog(@"peopleArray's Size = %d", [self.peopleArray count]);
}
}
return cell;
}
當我第一次加載視圖一切都很好。這是打印:
PERSON AT ROW 0 = Melissa
peopleArray's Size = 2
PERSON AT ROW 1 = Dave
peopleArray's Size = 2
後,我將某人添加到該數組我得到這個:
PERSON AT ROW 1 = Dave
peopleArray's Size = 3
PERSON AT ROW 2 = Tom
peopleArray's Size = 3
當我添加第二個人,我得到:
PERSON AT ROW 2 = Tom
peopleArray's Size = 4
PERSON AT ROW 3 = Ralph
peopleArray's Size = 4
爲什麼沒有打印陣列中的每個人?這種模式繼續下去,它只打印兩個人,它總是最後兩個人。我錯過了什麼?
--- 修訂 ---
確定。我的細胞沒有正確更新,我想我問的建議會幫助我。我想這不是主要問題。
我的問題是我的行沒有打印正確的信息。當視圖第一次加載我得到:
Melissa
Dave
,但我加湯後,我得到:
Melissa Dave
Melissa or Dave
Tom Tom
,我添加拉爾夫之後,我得到:
Melissa ?
Tom or ?
Tom Tom
Ralph Ralph
這是怎麼回事上?
如何更新表? – TommyG 2012-03-19 23:17:41
** Tableview沒有正確更新**。添加新項目時,您在應用中看到了什麼?你只談到了'NSLog'輸出,但沒有解釋實際UI上的問題。 – sch 2012-03-19 23:34:07
我想這個問題是細胞正確更新。當我打印'self.peopleArray'後,我添加每個人後,它顯示每個人。但是當它顯示在單元格中時,它不會正確顯示所有名稱。 – tazboy 2012-03-19 23:52:19