Dheeraj Dheeraj - 1 year ago 56
Objective-C Question

getting element from array one by one and createbutton

I have a UItextFeild as search bar on clicking a tableview with suggestion open.now i want to click on that row and make a button with its label in below view as described in image .how can i acheive this here is how i want

I want it one by one means when i click on row it create a button on view and UITableView hide then again i start searching and on again clicking on some other row make a button with different value of X.

_selectednames = [NSMutableArray arrayWithCapacity:self.sortedArray.count];

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
// MGContactItemsModel *model = _arrayForContacts[indexPath.row];
self.searchText.text = [NSString stringWithFormat:@"%@",[_sortedArray objectAtIndex:indexPath.row]];
self.tableView.hidden = YES;

[_selectednames addObject:_searchText.text];

for (int i; i < _selectednames.count ; i++) {
[self makeLabelsAndButtons:i];


now in my makeLabelsAndButtons funtion i done something like this

CGFloat xOffset = 10.0f;
int buttonCount = 0;

CGRect buttonFrame = CGRectMake(xOffset, 10.0, 50.0, 15.0);

NSString *nameString = [self.selectednames objectAtIndex:indexPath];

UIButton *_button = [UIButton buttonWithType:UIButtonTypeCustom];
[_button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
//_button.frame = CGRectMake(xOffset, 10.0, 50.0, 15.0);
for(int i=buttonCount; i >0;i++) {

xOffset = xOffset+10;

buttonFrame.origin.x += buttonFrame.size.width+xOffset;

_button.frame = buttonFrame;

_button.tag = indexPath;

//button.center = CGPointMake(xOffset, 10.0f);
//[button setBackgroundImage:[UIImage imageNamed:picString] forState: UIControlStateNormal];
[_button setTitle:self.selectednames[indexPath] forState:UIControlStateNormal];
_button.backgroundColor = [UIColor lightGrayColor];
_button.titleLabel.font = [UIFont fontWithName:@"Arial" size:8.0f];

_button.layer.cornerRadius = 10.0f;
_button.layer.masksToBounds = YES;

[self.buttonView addSubview:_button];
NSLog(@"name: %@", nameString);

also on clicking button it will removed.any help would be thankful.

Answer Source

It works for me.Please try it.


            for (int i=0; i<[arrAssignUsers count]; i++) {
                CGRect screenRect=[[UIScreen mainScreen]bounds];
                CGFloat screenWidth=screenRect.size.width;

                [arrBtnStatus addObject:[NSNumber  numberWithBool:NO]];

                NSString *strNames=[arrAssignUsers objectAtIndex:i];
                stringsize=[strNames sizeWithAttributes:
                            @{NSFontAttributeName: [UIFont systemFontOfSize:20.0f]}];
                btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
                CGFloat m=x+stringsize.width+10;
                CGFloat n=screenWidth-20;

                if (m<=n) {
                    btn.frame=CGRectMake(x, y,stringsize.width,stringsize.height);
                    x=x+stringsize.width +10;
                    btn.frame=CGRectMake(x, y,stringsize.width,stringsize.height);

                [btn setTitle:self.arrAssignUsers[i] forState:UIControlStateNormal];
                [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                [btn addTarget:self
                        action:@selector(notifyButtonPressedInEditTask:) forControlEvents:UIControlEventTouchUpInside];

                [btn.layer setMasksToBounds:YES];
                btn.layer.borderColor=[UIColor orangeColor].CGColor;

                [self checkNotifybtnStatus:btn];
                [cell addSubview:btn];

It may helps to you.Thank you

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download