开发者

Reuse Cell with Subview (programmatically)

开发者 https://www.devze.com 2023-03-12 11:40 出处:网络
I\'m having an issue programmatically adding a subview to a UITableView cell. I am attempting to add a UITextFiled to 2 cells and a UILabel to another cell in one section of my table. The table has 3

I'm having an issue programmatically adding a subview to a UITableView cell. I am attempting to add a UITextFiled to 2 cells and a UILabel to another cell in one section of my table. The table has 3 sections and most of it is manually created. One of the cells, when selected calls an action that inserts four more cells. If the same cells is selected again, the four cells will be removed. When this action occurs, the subviews get mixed up and are on the wrong cells. The keyboard also does not respond to the done key.

I try to create the subviews inside the if (cell == nil) conditional statement. Here is my code that creates the cells (sorry for it being so messy, I've been trying a ton of things):

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    NSLog(@"cell created");
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    if (indexPath.section == 0 && indexPath.row == 0 && ([cellText objectAtIndex:indexPath.row] == @"Weight")) {
        exerciseWeight = [[[UITextField alloc] initWithFrame:CGRectMake(0, 12, 295, 30)] autorelease];
        exerciseWeight.textAlignment = UITextAlignmentRight;
        exerciseWeight.adjustsFontSizeToFitWidth = YES;
        exerciseWeight.textColor = [UIColor blackColor];
        exerciseWeight.backgroundColor = [UIColor clearColor];
        exerciseWeight.tag = 89899;
        exerciseWeight.keyboardType = UIKeyboardTypeNumberPad;
        [exerciseWeight setReturnKeyType:UIReturnKeyDone];
        exerciseWeight.clearButtonMode = UITextFieldViewModeNever;
        [exerciseWeight setEnabled: YES];
        [exerciseWeight setDelegate:self];
        [cell addSubview:exerciseWeight];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;

    } else if (indexPath.section == 0 && indexPath.row == 1) {
        NSLog(@"Created metric subview");
        metric = [[[UILabel alloc] initWithFrame:CGRectMake(100, 5, 180, 30)] au开发者_如何学Pythontorelease];
        metric.textAlignment = UITextAlignmentRight;
        metric.textColor = [UIColor grayColor];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        metric.tag = 89898;
        [cell addSubview:metric];
    } else if (([cellText count] - 1) == indexPath.row){
        exerciseReps = [[[UITextField alloc] initWithFrame:CGRectMake(0, 12, 295, 30)] autorelease];
        exerciseReps.textAlignment = UITextAlignmentRight;
        exerciseReps.adjustsFontSizeToFitWidth = YES;
        exerciseReps.backgroundColor = [UIColor clearColor];
        exerciseReps.tag = 89890;
        exerciseReps.keyboardType = UIKeyboardTypeNumberPad;
        [exerciseReps setReturnKeyType:UIReturnKeyDone];
        exerciseReps.clearButtonMode = UITextFieldViewModeNever;
        [exerciseReps setEnabled:YES];
        [exerciseReps setDelegate:self];
        [cell addSubview:exerciseReps];
    } else if (indexPath.section == 2) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
} else {
    exerciseWeight = (UITextField *)[cell viewWithTag:89899];
    metric = (UILabel *)[cell viewWithTag:89898];
    exerciseReps = (UITextField *)[cell viewWithTag:89890];
}
if (indexPath.section == 0) {
    cell.textLabel.text = [cellText objectAtIndex:indexPath.row];
}
// Configure the cell...
if (indexPath.section == 0 && indexPath.row == 0) {
    cell.textLabel.textAlignment = UITextAlignmentLeft;
    exerciseWeight.placeholder = @"160";


} else if (indexPath.section == 0 && indexPath.row == 1) {
    metric.text = metricUnit;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} else if (cell.textLabel.text == @"Repetitions"){
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryNone;
    exerciseReps.placeholder = @"10";
} else if (indexPath.section == 1) {
    cell.textLabel.text = @"Log New Set";
    cell.textLabel.textAlignment = UITextAlignmentCenter;
    cell.accessoryType = UITableViewCellAccessoryNone;
} else if (indexPath.section == 2) {
    cell.textLabel.text = @"History";

} else if (cell.textLabel.text == @"  Pounds") {
    cell.textLabel.textColor = [UIColor grayColor];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
} else if (cell.textLabel.text == @"  Kilograms") {
    cell.textLabel.textColor = [UIColor grayColor];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
} else if (cell.textLabel.text == @"  Miles") {
    cell.textLabel.textColor = [UIColor grayColor];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
} else if (cell.textLabel.text == @"  Kilometers") {
    cell.textLabel.textColor = [UIColor grayColor];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
}

return cell;

// [exerciseWeight release];
// [metric release];
// [exerciseReps release]; 
}


looks like you're going to have some fun here. But it will be satisfying once you're done :-)

I'm not sure the code you posted is the bit you need to be worrying about. Defining what you see in your table (primarily) are methods like heightForRowAtIndexPath, numberOfSectionsInTableView and numberOfRowsInSection. If you get those right you will get the display you want. So when you tap a cell you will be modifying your underlying data structures, then calling reloadData - causing the table view to call those methods all over again.

Regarding laying out those cells I don't see you using the cell's contentView at all. Have you taken a look at the Cocoa With Love blog, specifically Easy custom UITableView drawing and UITableView construction, drawing and management (revisited)? I have also used the xib method of defining table view cells as described in the Apple docs, Table View Programming Guide for iOS and given the way Xcode just keeps improving interface builder's interaction with code would probably continue to do so. I was very much against IB but now see the value in using it since Xcode 4 took away the pain of switching apps to use it, there are a lot fewer opportunities to make mistakes.

What I'm thinking is that when you "add a view" to a cell you should just switch to another cell that you have defined and load the cell, after reloadData, as required. When you want to add cells you change your underlying data structures and let the table display itself.

On the question of cell identifiers - you can either use one cell identifier and save only the cost of creating a cell, factoring out everything common that you do to all your cells, OR you can create cells with a unique identifier per type of cell and reuse cell variant TK421 when your section/row indicates a TK421 is required. I tend to do the second. I don't actually know which one performs better, but I'm sure that either is better than no reuse at all.

0

精彩评论

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