开发者

Textfield in appearing multiple times UItableViewGrouped

开发者 https://www.devze.com 2023-03-10 21:03 出处:网络
I found some example code of how to implement a textfield into a cell here: Having a UITextField in a UITableViewCell

I found some example code of how to implement a textfield into a cell here: Having a UITextField in a UITableViewCell

However the textfield appears multiple times on my table in the 1st section of the table, even though i specified it to only appear when it comes up to the 2nd section of the table and the first row of that section.

Any explanations why this happens? I only want it in the 2nd section 1 st row. but it appears to think that whenever the 2nd section is coming up it will just draw the textfield in advance. Is there a way to "lock" it to a particular group and cell?

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [self CreateMultilinesCell:CellIdentifier];
}
NSLog(@"%d", [indexPath section]);
if ([indexPath section] == 1) {
    NSLog(@"TextField");

  开发者_运维知识库  if ([indexPath row] == 0 ) {
        UITextField *replyTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 185, 30)];
        replyTextField.adjustsFontSizeToFitWidth = YES;
        replyTextField.textColor = [UIColor blackColor];

        replyTextField.keyboardType = UIKeyboardTypeDefault;
        replyTextField.returnKeyType = UIReturnKeyDone;
        replyTextField.backgroundColor = [UIColor grayColor];
        replyTextField.autocorrectionType = UITextAutocorrectionTypeNo; 
        replyTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
        replyTextField.textAlignment = UITextAlignmentLeft;
        replyTextField.tag = 0;
        replyTextField.delegate = self;

        replyTextField.clearButtonMode = UITextFieldViewModeNever;
        [replyTextField setEnabled: YES];

        [cell.contentView addSubview:replyTextField];

        [replyTextField release];
        cell.detailTextLabel.text = @"something";

    }
}
else {
    cell.detailTextLabel.text = [separatedString objectAtIndex:indexPath.row];
}

return cell;
}

createmultilinecell:

- (UITableViewCell*) CreateMultilinesCell :(NSString*)cellIdentifier
{
//NSLog(@"Entering CreateMultilinesCell");
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                                reuseIdentifier:cellIdentifier] autorelease];

cell.detailTextLabel.numberOfLines = 0;
cell.detailTextLabel.font = [self SubFont];
cell.detailTextLabel.textColor = [UIColor colorWithRed:10.0/255 green:10.0/255 blue:33.0/255 alpha:1.0];
[cell setBackgroundColor:[UIColor clearColor]];//]colorWithRed:.98 green:.98 blue:.99 alpha:1.0]];
[self.tableView setBackgroundColor:[UIColor clearColor]];//colorWithRed:.94 green:.96 blue:.99 alpha:1.0]];
                                                         //NSLog(@"Exiting CreateMultilinesCell");
return cell;
}

argh i'm too low to answer my own question so i'll just update here:

Looks like making 2 cellidentifiers work. Thanks. Just learnt something new! haha.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
NSString *replyCellIdentifier = @"replyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UITextField *replyTextField;
if (cell == nil) {
    if ([indexPath section] == 0) {
        cell = [self CreateMultilinesCell:CellIdentifier];      
    }
    else if ([indexPath section] == 1) {
        NSLog(@"TextField");
        cell = [self CreateMultilinesCell:replyCellIdentifier];     
        if ([indexPath row] == 0) {
            replyTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 185, 30)];
            replyTextField.adjustsFontSizeToFitWidth = YES;
            replyTextField.textColor = [UIColor blackColor];
            replyTextField.keyboardType = UIKeyboardTypeDefault;
            replyTextField.returnKeyType = UIReturnKeyDone;
            replyTextField.backgroundColor = [UIColor grayColor];
            replyTextField.autocorrectionType = UITextAutocorrectionTypeNo; 
            replyTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
            replyTextField.textAlignment = UITextAlignmentLeft;
            replyTextField.tag = 0;
            replyTextField.delegate = self;

            replyTextField.clearButtonMode = UITextFieldViewModeNever;
            [replyTextField setEnabled: YES];

            [cell.contentView addSubview:replyTextField];

            [replyTextField release];
            cell.detailTextLabel.text = @"something";
        }           
    }
    /*else {
        cell.detailTextLabel.text = [separatedString objectAtIndex:indexPath.row];
    }*/
}
NSLog(@"%d", [indexPath section]);
if ([indexPath section] == 0) {
    cell.detailTextLabel.text = [separatedString objectAtIndex:indexPath.row];

}
return cell;
}

thanks to everyone that contributed.

In terms of functionality i'm not so sure yet but it's one step closer to getting where i want it to be :).


Try having two different cells, one with a textField and one without. Use different CellIdentifier strings for the two different types of cells. That should resolve it.


Thats because you have this part of your code outside the if(cell == nil)

if ([indexPath row] == 0 ) {
    UITextField *replyTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 185, 30)];
    ...........

}

So this line of code is always called when you scroll the table view to see that row and thus adding uitextfield every time, so what you can do

Declare that variable in this block if(cell == nil){....}

Also if you are not adding or deleting any rows or section in your table view than it will work just fine.

*PS.*It is alot harder to handle tableview when you are using it to show dynamic data that user can edit.

You will have to do more than that but for now I think you are good to get started.


Try this if your using section

NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row]

and if you are not using section then use

NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%i",indexPath.row];

0

精彩评论

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

关注公众号