質問

So I have this code

if (tableView.tag == RECO_TABLE) {
            CustomCell  *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            NSString *roomName = [selectedRooms objectAtIndex:indexPath.row];
            [cell.cellRoomName setText:roomName];
       return cell;
    }

And Inside my Custom Cell is

@property (weak, nonatomic) IBOutlet UILabel *cellRoomName;
@property (weak, nonatomic) IBOutlet UITextField *cellRoomPrice; 

cellRoomName is fine but cellRoomPrice keeps duplicating when my row reached >5 and I know the concept of reuse cell but I don't know how to start.

btw here's my sample scenario

Row 1 = 40

Row 2 = 2

Row 3 = null

Row 4 = null

Row 5 = null

Row 6 = 40

Row 7 = 2

So every 5th row my row gets duplicated.

役に立ちましたか?

解決

Looks like you are not setting the value of cellRoomPrice properly. If you already have this value for each row, you need to set it for each cell/row.

Since you are allowing user to edit room price, you need to store that value somewhere, preferably in an array indexed by row number. Assuming you have a UITextFieldDelegate, you also need to know the row number the user is editing(to store the value) on -textFieldDidEndEditing: which can be resolved by setting a tag on -cellForRowAtIndexPath:

Hope this helps.

他のヒント

There are few changes in code you need to do first of check for cell is nil or not

if (tableView.tag == RECO_TABLE) {
    CustomCell  *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
    if(!cell) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomCell"];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    NSString *roomName = [selectedRooms objectAtIndex:indexPath.row];
    [cell.cellRoomName setText:roomName];
    return cell;
}

And then also the text you are typing in cell's text field need to store in some other array say's "selectedRoomsPrices" and in "cellForRowAtIndexPath:" you need to check for counts and set that value as well in "cell.cellRoomPrice" like this :

if (tableView.tag == RECO_TABLE) {
    CustomCell  *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
    if(!cell) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomCell"];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    NSString *roomName = [selectedRooms objectAtIndex:indexPath.row];
    [cell.cellRoomName setText:roomName];
    NSString *roomPrice = @"";
    if(selectedPrices.count > indexPath.row) {
        roomPrice = [selectedRoomsPrices objectAtIndex:indexPath.row];
    }
    [cell.cellRoomPrice setText:roomPrice];

    return cell;
}

Try this because you are not checked cell is allocated or not.

if (tableView.tag == RECO_TABLE) {
    CustomCell  *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
    if(!cell) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomCell"];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }

    NSString *roomName = [selectedRooms objectAtIndex:indexPath.row];
    [cell.cellRoomName setText:roomName];

    return cell;
}

Your row reached > 5 is duplicated because i don't set value for it! TableView reuse before cell so if you don't set new value for it, tableview will use before cell!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top