0

Iam trying to develop UITableView with a multiline textfield in its cell. I created UITableView and cell programmatically and at this moment i added one normal UITextField but i really want something like whats app chat typing field so when i type more characters it will increase its height and UITableViewCell height automatically. (pls check attached image)

My UITableView Delegate methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    {
        return 1;    //count of section
    }

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath      *)indexPath;
    {

        return 100;
    }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [selectedTabFields count];
    }


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

        cell= [tableView1 dequeueReusableCellWithIdentifier:@"cell"];

        if (cell == nil)
        {
            cell = [[homeCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:MyIdentifier];
               cell.selectionStyle=UITableViewCellSelectionStyleNone;


                   [cell.contentView addSubview:[self getIntegerTextfield:indexPath.row]];

        }




        return cell;

    }

    //Integer Textfield
    -(UITextField *)getIntegerTextfield:(NSUInteger)index{

        UITextField *textField= [[UITextField alloc] initWithFrame:CGRectMake(15,40,cell.frame.size.width-30,cell.frame.size.height)];
        textField.placeholder=@"Text here";
        textField.tag=index;
        textField.delegate=self;

        textField.keyboardType=UIKeyboardTypeNumbersAndPunctuation;
        textField.textAlignment=UITextAlignmentLeft;
        textField.font= [UIFont fontWithName:@"Helvetica-Neue Light" size:14];
        textField.textColor=[UIColor darkGrayColor];
        textField.backgroundColor=[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1];
        return textField;
    }

Even it tried this code but its not working, when i run with this changes UITableView Cell was empty. Tried the same, but not working Pls suggest answer.

Attached images

Current Image

Whats app multiline

Community
  • 1
  • 1
Bangalore
  • 1,572
  • 4
  • 20
  • 50

2 Answers2

2

Why reinvent the wheel when you have third party libraries? This one fits your requirements perfectly. Just use a UITextView instead.

Edit: Since you say the above one does not work for you for some reason. You can use others like SlackhQTextViewController etc.

NSNoob
  • 5,548
  • 6
  • 41
  • 54
  • @Bangalore most likely you are doing something wrong in integration. If you don't feel comfortable using this one, You can use Bharat's answer Or look on google to find other pods to perform the same job. There are plenty. – NSNoob Nov 03 '15 at 08:40
  • The main point is, you don't have to waste time in reinventing the wheel when other people have done it for you in very stable ways. – NSNoob Nov 03 '15 at 08:40
  • I have added another alternative in my answer @Bangalore. – NSNoob Nov 03 '15 at 08:51
0

Add in getIntegerTextfield:(NSUInteger)index{

textField.frame.size.height = (textField.text.length/a) * b;

a = size of textField you added in xib. b = height of 1 line textfield.

S.Jain
  • 441
  • 4
  • 9
  • i created this textfield programmatically based on number of rows – Bangalore Nov 03 '15 at 10:39
  • You have to set heights of both Textfield and row height. Currently you are setting only height of Row, but not TextField – S.Jain Nov 03 '15 at 10:50
  • how ?i didnt understand? – Bangalore Nov 03 '15 at 10:51
  • - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [selectedTabFields count]; } will only set row height. By default the textfield is a oneline Object. You have to set the height of textfield also. – S.Jain Nov 03 '15 at 10:53
  • that method is for setting row number not row height – Bangalore Nov 03 '15 at 10:55
  • yes, table view row height you have set to 100, but you need to focus on textfield height – S.Jain Nov 03 '15 at 11:07