从表格单元格编辑对象

时间:2013-10-21 23:28:46

标签: ios uitableview

我有一个显示所有对象的表视图,并且可以删除对象,现在我需要获取它,以便我可以从NSMutableArray编辑对象并保存和重新发送表数据。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    AssignmentInfo *ai = [self.alist objectAtIndex:indexPath.row];
    UILabel *classname = (UILabel *)[cell viewWithTag:1];
    UILabel *assignementText = (UILabel *)[cell viewWithTag:2];
    UILabel *date = (UILabel *)[cell viewWithTag:3];

    classname.text = ai.className;
    assignementText.text = ai.assignmentTitle;
    date.text = ai.dateTimeString;
    // Configure the cell...

    return cell;
}

如果需要,这是整个项目的链接:

http://www.abdelelrafa.com/AssignmentAppTwo.zip

2 个答案:

答案 0 :(得分:2)

首先控制 - 将您的单元格拖动到故事板中的AddEditViewController(执行模态segue)。接下来选择segue并将其命名为“edit”。之后转到AddEditViewController.h并创建以下属性:

@property (nonatomic)BOOL edit;
@property (nonatomic,strong)AssignmentInfo *assignmentEditing;

之后在prepareForSegue的AssignmentListViewController.m中添加:

if ([segue.identifier isEqualToString:@"edit"]) {
    UITableViewCell *cell = sender;
    AddEditViewController *addEditController = segue.destinationViewController;
    [addEditController setOtherdelegate:self];
    addEditController.edit = YES;
    AssignmentInfo *a = [self.alist objectAtIndex:[self.tableView indexPathForCell:cell].row];
    addEditController.assignmentEditing = a;
    [self.alist removeObject:a];
}

最后在viewDidLoad的AddEditViewController.m中添加:

if (self.edit) {
        self.className.text = self.assignmentEditing.className;
        self.assignmentTitle.text = self.assignmentEditing.assignmentTitle;
        self.assignmentDescription.text = self.assignmentEditing.assignmentDescription;
        NSString *string = self.assignmentEditing.dateTimeString;
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
        dateFormatter.timeZone = [NSTimeZone defaultTimeZone];
        dateFormatter.timeStyle = NSDateFormatterShortStyle;
        dateFormatter.dateStyle = NSDateFormatterShortStyle;
        NSDate *date = [dateFormatter dateFromString:string];
        dateTimePicker.date = date;
}

答案 1 :(得分:-1)

如果您不想要自定义动画,只需从NSMutableArray中删除该对象,然后调用reloadData对象的UITableView方法。