我有UITableView
个自定义单元格。每个单元格有3个textfields
,其中包含 - 姓名,年龄,出生日期。每个单元格都有一个十字按钮,单击该按钮将删除该单元格。此外,显示的最后一个单元格还有一个加号按钮,单击该按钮将在其下方生成一个新单元格。现在,最近创建的最后一个单元格将具有加号按钮。
我有一个模型类"StudentInfo"
,它表示每个单元格中存在的数据。在包含此tableView的ViewController.m
文件中,我将数据保存在NSMutableDictionary
中。保存数据的适当方法是什么?
在我的情况下,我有一个NSMutableDictionary
,其中包含行号作为键,学生信息作为值。每当按下加号按钮时,我都会保存可见的单元格。此外,当按下删除按钮时,我从模型中删除相应的键,并更改字典以反映新信息。
在didEndDisplayingCell:forRowAtIndexPath
方法中,数据也会保存。
此实施目前是错误的。处理这种情况的适当方法是什么?
-(void) saveVisibleCellsData
{
NSArray *cells = [myTable visibleCells];
for (UITableViewCell *cell in cells)
{
NSIndexPath *indexPath = [myTable indexPathForCell:cell];
[self saveCellFromViewToModel:cell forRowAtIndexPath:indexPath];
}
}
- (IBAction)btnPlusPressed:(id)sender
{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:myTable];
NSIndexPath *indexPath = [myTable indexPathForRowAtPoint:buttonPosition];
BOOL isDataValid = [self validateDataInCellAtIndexPath:indexPath];
if (isDataValid) {
NSArray *cells = [myTable visibleCells];
for (UITableViewCell *cell in cells)
{
NSIndexPath *indexPath = [myTable indexPathForCell:cell];
[self saveCellFromViewToModel:cell forRowAtIndexPath:indexPath];
}
++numOfItems;
[myTable reloadData];
}
}
- (IBAction)deleteCellButtonPressed:(id)sender
{
UIButton *button = (UIButton *)sender;
UITableViewCell *tmpCell = (UITableViewCell *)[[button superview] superview];
NSIndexPath *indexPath = [myTable indexPathForCell:tmpCell];
rowBeingDeleted = indexPath.row;
NSArray *cells = [myTable visibleCells];
for (UITableViewCell *cell in cells)
{
NSIndexPath *indexPath = [myTable indexPathForCell:cell];
[self saveCellFromViewToModel:cell forRowAtIndexPath:indexPath];
}
NSArray *tmpArry = [[NSArray alloc] initWithObjects:indexPath, nil];
--numOfItems;
[myTable deleteRowsAtIndexPaths:tmpArry withRowAnimation:UITableViewRowAnimationRight];
NSString *keyForLastObject = [NSString stringWithFormat:@"%zd", ([dictionaryWithCellsData count]-1)];
NSInteger initCount = [dictionaryWithCellsData count];
for (NSInteger counter = indexPath.row ; counter < initCount ; counter++)
{
NSString *keyCounter = [NSString stringWithFormat:@"%zd", counter];
NSString *keyCounterPlusOne = [NSString stringWithFormat:@"%zd", (counter+1)];
if ((counter+1) != initCount)
{
id tmpObject = [dictionaryWithCellsData objectForKey:keyCounterPlusOne];
[dictionaryWithCellsData setObject:tmpObject forKey:keyCounter];
}
}
[dictionaryWithCellsData removeObjectForKey:keyForLastObject];
[myTable reloadData];
}
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (rowBeingDeleted == -1)
{
[self saveCellFromViewToModel:cell forRowAtIndexPath:indexPath];
}
else if (rowBeingDeleted == indexPath.row)
{
rowBeingDeleted = -1;
}
}
saveCellFromViewToModel
函数存储学生信息。反对在NSMutableDictionary中传递的行号。
答案 0 :(得分:0)
你可以做以下事情
1)步骤是创建NSArray
学生信息对象,而不是将其存储在NSDictionary
2)如果您使用webservice来获取数据。要获取数据,您应该在视图中发送请求didLoad&amp;收到响应后将其保存在objects中。制作Object数组。之后重新加载你的tableView&amp;将数据应用于单元格
3)当您按下十字按钮时,请删除此行
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexToRemove inSection:indexPath.section]] withRowAnimation: UITableViewRowAnimationRight];
4)删除行时,也要从数组中删除相应的对象。 在特定索引路径处获取该对象。
[arrayStudents removeObjectIdenticalTo:studentInfo];
5)您可以在页脚视图中添加加号,而不是添加加号按钮