开发者

Help with UITableView and NSArray

开发者 https://www.devze.com 2023-02-02 01:10 出处:网络
I\'m trying to create a really simple to do list using an array and a tableview. When the user hits the + button, the insert method is called and a UIAlert is presented so the user can input text. Not

I'm trying to create a really simple to do list using an array and a tableview. When the user hits the + button, the insert method is called and a UIAlert is presented so the user can input text. Nothing crashes, but after the user hits OK, nothing is added to the tableview, however the log does display the code it's supposed to. Can anyone detect what's wrong? (I think I've made all the right connections in IB as well)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {   
    data=[[NSMutableArray alloc]init];

    // Override point for customization after application launch.

    [self.window makeKeyAndVisible];

    return YES;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [data count];

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if(cell==nil)
        cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]autorelease];
    cell.textLabel.text=[data objectAtIndex:indexPath.row];
    return cell;
}

-(IBAction)insert {
    UIAlertView* dialog = [[UIAlertView alloc] init];
    [dialog setDelegate:self];
    [dialog setTitle:@"Enter Name"];
    [dialog setMessage:@" "];
    [dialog addButtonWithTitle:@"Cancel"];
    [dialog addButtonWithTitle:@"OK"];

    UITextField *nameField = [[UITextField alloc] 
                              initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
    [nameField setBackgroundColor:[UIColor whiteColor]];
    [dialog addSubview:nameField];
    [nameField release];
    [dialog show];
    [dialog release];
}


- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{       
    if (buttonIndex != 0)  // 0 == the cancel button
    {
        //  Since we don't have a pointer to the textfield, we're
        //  going to need to traverse the subviews to find our 
        //  UITextField in the hierarchy
        for (UIView* view in alertView.subviews)
        {
            if ([view isKindOfClass:[UITextField class]])
            {
                UITextField* textField = (UITextField*)view;
                [data addObject:textField.text == nil ? @"moha" : textField.text];
                NSLog(@"text:[%@]", textField.text);
                break;
            }
        }
    }
}
-(IBAction)toggleEdit {
    UIBarButtonItem *leftItem;
    [mainTableView setEditing:!mainTab开发者_如何转开发leView.editing animated:YES];

    if (mainTableView.editing) {
        leftItem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(toggleEdit)];
    }else {
        leftItem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(toggleEdit)];
    }

    navItem.leftBarButtonItem=leftItem;

}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [data removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];

}


You need to force the tableview to reload its data after modifying the array:

[self.tableView reloadData];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号