开发者

How to get the data from custom tableview cell

I'm new to Iphone app dev. I struck up with a problem. I'm having the table view in that i inserted textfield in each row. I done the UI Part. But how can i get the values from the textfield in tableview. I created customCell class . I could not use IBOutlet and all.

here is my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[CustomCell alloc] initWithFrame:CGRec开发者_JS百科tZero reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...

    return cell;
}


You should have a dataSource (usually a NSMutableArray) that (usually) contains the values for each cell. You should be able to get the value you need by using the indexPath, e.g.

NSObject *object = [dataArray objectAtIndex:indexPath.row];

In the above code I assume the dataSource for the tableView is an array called dataArray. I also assume it contains objects of the type NSObject (this usually isn't true in real world examples, in such situations it's generally a subclass like NSDictionary or a custom NSObject subclass.

Make sure the tableView is connected to it's dataSource. The connection is done by (at minimum) the methods setDataSource: on your UITableView instance and numberOfSectionsInTableView: and numberOfRowsInSection: delegate methods.

Your UITableViewCell subclass should generally not be used to save data in, if you use it in this way, you're taking the wrong approach. The following site should be a decent introduction in using the UITableView class: http://www.mobisoftinfotech.com/blog/iphone/introduction-to-table-view/


As Schreurs already explained you need to implement the UITextFieldDelegate protocol to your viewController (along with UITableViewDataSource), look into those in the documentation to learn more of what you can do with them. But this is more tricky than having distinct UITextFields in your view.

You have to consider the fact that when a cell leaves the visible range of the tableview it will be released or reused. So if for example a cell 1 contains a textfield your write something in it and then scroll to cell 15, you're gonna probably get a cell with the textfield of cell 1 and its contents. If you prepare your cells for reuse, emptying the textFields you have to keep that data somewhere to re-enter it in the proper cell. After all that you're gonna scratch your head what textField is calling your delegate (probably your viewController, so you're gonna have to tag them with a number from which you can extract a row number - i.e. cell.textField.tag = indexPath.row + 100).

So to sum up you want something like this in your viewController

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ([textField.text length] > 0) {
        NSUInteger row = textField.tag - 1;
        [textFieldValues setObject:textField.text forKey:[NSNumber numberWithInt:row]];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"cellId";
    TextFieldTableViewCell *cell = (TextFieldTableViewCell *) [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell)
        cell = [[[TextFieldTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId] autorelease];

    cell.textField.tag = indexPath.row + 1;
    cell.textField.delegate = self;
    NSString *value = [textFieldValues objectForKey:[NSNumber numberWithInt:indexPath.row]];
    if (value)
        cell.textField.text = value;
    else
        cell.textField.text = @"";

    return cell;
}

and then in your TextFieldTableViewCell.h

@property (nonatomic, readonly) UITextField *textField;

and finally in your TextFieldTableViewCell.m

@synthesize textField;

p.s. I was wandering what might happen when the editing textField leaves the visible cell range, and it isn't reused or released... gave me the chills! So didEndEditing should be adequate.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜