views:

364

answers:

1

I am trying to programatically add a UITextFiled inside one of my tableview cells. How would I do this?

Assuming it is in the following method, what code would I use?

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
        cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
        // Don't let the user click when not in editing mode
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    switch (indexPath.row) {
        case 0: 
            cell.textLabel.text = @"Title";
            // This is where I want to add a text field
            cell.detailTextLabel.text = @"test";
            cell.editing = YES;
        break;
    }

    return cell;
}
+1  A: 

try this:

[cell.contentView addSubview:[[[UITextField alloc] initWithFrame:CGRectMake(...)] autorelease]];
Yakov
Ok, that works. But now, how would I have a pointer to that text field, so I can do things like give it default text, and an outlet?
Nic Hubbard
sure you can have a pointer to that text field. For example u would create pointer-class member, create UITextField instance into constructor than add created instance in cell. ex: [cell.contentView addSubview:instance];
Yakov
Ok, I have done what you said, but the text field is not showing, I tried to create the UITextField instance and assign it to my pointer-class member using rideTitle = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)]; but that does not seem to work. Ideas?
Nic Hubbard