简体   繁体   中英

UITableViewCell & UITextField

I'm trying to make an UITableView with prototype cells. Now i'm getting into a problem. I use 3 kind of different prototype cells, and in 2 of them there is a UITextField.

I'm doing it on this way, i have subclassed the UITableViewCell 2 times, for the Outlets of my UITextFields for getting the information out of the Fields.

Am i'm doing it on the right way? Or do i need to redo my approach?

Here is my code [UPDATE]:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier;

    switch (indexPath.section) {
        case 0:{
            CellIdentifier = @"nameCell";
            naamCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil) {
                cell = [[naamCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            }

            [cell setSelectionStyle: UITableViewCellSelectionStyleNone];

            return cell;
            break;
        }

        default:{
            if(indexPath.row == [[_diceArray objectAtIndex:indexPath.section]count]){
                CellIdentifier = @"addCell";

            }else{
                CellIdentifier = @"optionCell";
                optionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

                if (cell == nil) {
                    cell = [[optionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
                }
                [[cell optionCell] setText:[NSString stringWithFormat:@"%@", [[_diceArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]]] ;
                [cell setSelectionStyle: UITableViewCellSelectionStyleNone];

                return cell;
            }

            break;
        }
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    [cell setSelectionStyle: UITableViewCellSelectionStyleNone];

    return cell;
}

EDIT:

So here is a picture of what is happening. So i have 2 sections. Added like 2 things in my textfields there. Delete one from section 2. The content from the UITextField goes to section 3 and doesn't get deleted: 错误

EDIT 2: I've updated the code. So people can dynamically edit the UITextField. I'm getting there i think, tested it with presented NSStrings in my Array, then Cell.textfield.text = text from array from index of section.

Now i need to find a way that the user can add text on it's own, and when pressing or changing textfield the text get's saved to my array and then do [Table ReloadData]; Or is this not the right way to do it?

I think i have finally gotten it!

Needed to implement this:

- (void) textFieldDidEndEditing:(UITextField *)textField {
    UIView* v = textField;

    do {
        v = v.superview;
    } while (![v isKindOfClass: [UITableViewCell class]]);
    optionCell* cell = (optionCell*)v;

    NSIndexPath* ip = [self._tabel indexPathForCell:cell];

    if (![textField.text isEqual:[[_diceArray objectAtIndex:ip.section] objectAtIndex:ip.row]]) {
        [[_diceArray objectAtIndex:ip.section] setObject:textField.text atIndex:ip.row];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM