Domanda

I have this collapsable UITableView where there is a UITextField and a UIButton in the last cell in each table section. I would like to send the text in the UITextField to the function that is called by the UIButton that is next to it in the same cell, but I am baffled in how to achieve this. Thanks in advance for the help!

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

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


    if ([self tableView:_tableView canCollapseSection:indexPath.section])
    {
        int num = 1;
        if (self.chat[indexPath.section - 1][@"num"] != nil)
            num = [self.chat[indexPath.section - 1][@"num"] intValue] + 1;

        if (!indexPath.row)
        {
            cell.textLabel.text = self.chat[indexPath.section - 1][@"msg"]; // only top row showing

            if ([expandedSections containsIndex:indexPath.section])
            {
                cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
            }
            else
            {
                cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];
            }
        }
        else if (indexPath.row < num && indexPath.row >= 1)
        {
            cell.textLabel.text = self.chat[indexPath.section - 1][key];
            cell.accessoryView = nil;
        }
        else
        {
///////////////////////////////////////////
////////This is the important part/////////
///////////////////////////////////////////
            UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(14, 6, 245, 31)];

            self.sendButton = [[UIButton alloc] initWithFrame:CGRectMake(265, 1, 50, 40)];
            [self.sendButton setTitle:@"Reply" forState:UIControlStateNormal];
            [self.sendButton setTitleColor:UIColorFromRGB(0x960f00) forState:UIControlStateNormal];
            [cell addSubview:self.sendButton];

            [self.sendButton addTarget:self action:@selector(sendReply:) forControlEvents:UIControlEventTouchUpInside];

            [cell addSubview:field];
            cell.accessoryView = nil;


        }
    }
    else
    {
        cell.accessoryView = nil;
        cell.textLabel.text = @"Normal Cell";

    }

    return cell;
}
È stato utile?

Soluzione

Give some unique tag to textfield and button by below way:

/////////////////////////////////////////// ////////This is the important part///////// ///////////////////////////////////////////

UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(14, 6, 245, 31)];

    self.sendButton = [[UIButton alloc] initWithFrame:CGRectMake(265, 1, 50, 40)];
    [self.sendButton setTitle:@"Reply" forState:UIControlStateNormal];
    [self.sendButton setTitleColor:UIColorFromRGB(0x960f00) forState:UIControlStateNormal];
    self.sendButton.tag = indexPath.section *1000 + indexPath.row;
    [cell addSubview:self.sendButton];

    [self.sendButton addTarget:self action:@selector(sendReply:) forControlEvents:UIControlEventTouchUpInside];

    field.tag = self.sendButton.tag + 1;
    [cell addSubview:field];
    cell.accessoryView = nil;

Now on button event,

-(void) sendReply:(UIButton *)sender
{
UITextField *field = [YOOUR_TABLE_VIEW viewWithTag:sender.tag + 1];
//Do you coding here
}

Altri suggerimenti

Make a Custom UITableViewCell that has uitextfield and a button on it and make a protocol/delegate of that custom uitableviewcell you've created.. so you can have more control of your code and the event of your button in the future..

check this tutorial: http://www.codigator.com/tutorials/ios-uitableview-tutorial-custom-cell-and-delegates/

cheers

For this,

In cellForRowAtIndexPath: method where you are allocating the send button add one line just to give some identifier to send button as below,

[self.sendButton setAccessibilityIdentifier:[NSString stringWithFormat:@"%d#%d",indexPath.row,indexPath.section]];

Now, in sendReply: method,

//First get the row and section information
NSString *str=[sender accessibilityIdentifier];
NSArray *arr=[str componentsSeparatedByString:@"#"];
//Get the index path
NSIndexPath *iRowId =[NSIndexPath indexPathForRow:[[arr objectAtIndex:0] intValue] inSection:[arr objectAtIndex:1] intValue]];

//get the cell
UITableViewCell *objCell=(UITableViewCell*)[tblviwBasketCell cellForRowAtIndexPath:iRowId];

Now objCell will be the cell in which you have added the button and text view. so get the subviews of objCell and access the textfield to get the text.

As you say, there is a text field in each section. You should set the tag of your UIButton and UITextField same as indexPath.section.

Then, in the target method, use this tag value to get the relevant cell from the relevant section, and iterate over it's subviews to get your textField.

In the target method, you should do something like this:

- (void) sendReply:(id)sender {

    int section = [(UIButton*)sender tag];

    int row = [myTableView numberOfRowsInSection:section] - 1;//assuming it is the last cell in the section that contains your textField.

    NSIndexPath* indexPath = [NSIndexPath indexPathForRow:row inSection:section];

    UITableViewCell* cell = [myTableView cellForRowAtIndexPath:indexPath];

    for (UIView* vu in cell.subviews) {

         if ([vu isKindOfClass:[UITextField class]]) {

                NSString* textString = [(UITextField*)vu text];

                //perform any tasks you want with the text now
            }
    }
}

You can simply use viewWithTag:, since it does an iterative search, but all the extra code is to avoid iterating over all the cells before reaching your relevant cell.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top