UITableView Prototype Cell with UIButton

With the introduction of iOS 5, UITableview now comes with Prototype Cell.

For those who work with anything before this, you will know that creating a customized tableview cell take alot of effort.

Today, I am going to show 2 method of getting the instance of the button that is clicked.

Method 1 is the “downright dirty” way of doing things. Method 2 is much “cleaner”.

Method 1:

In the

– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

We add the uibutton to the cell and add the indexpath.row to the tag of the UIButton.

UIButton *newButton = [[UIButton alloc ] initWithFrame:CGRectMake(0, 0, 100, 50)];

newButton.tag = indexPath.row;

[newButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

[cell.contentView addSubview:newButton];

By adding the indexPath.row to the tag of the button allow us to determine which row in the UITableview the user selected.

Therefore in the ButtonPressed Method we can do the following.

-(void) buttonPressed:(id) sender{

//We load the data from a NSArray name array_data

[array_data objectAtIndex:[sender tag]];

….}

As you can see from the above we can therefore know which data is selected and the application can progress forward.

Method 2:

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

    static NSString *CellIdentifier = @”Cell”;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }    

    UIButton *btn = (UIButton *)[cell viewWithTag:1];

    [btn addTarget:self action:@selector(onClicked:) forControlEvents:UIControlEventTouchUpInside];

}

The above is similar to the first method. Do note that the cell viewWithTag this have been already added to the Prototype cell in the Storyboard.

The tag can be set via the Storyboard, on the right of the Xcode, 3 tag from the right. The Tag Field.

Now in the onClicked Method.

-(void) onClicked: (id) sender{

    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];

    NSIndexPath *clickedButtonPath = [self.tableview indexPathForCell:clickedCell];

  }

In the clickedButtonPath, this allow us to find out the row that was selected by the user.

Summary

The 2 method above shows the different way of doing things, each have its pro and con.

In method 1, by setting the tag of the uibutton we remove certain customising ability should we want to change the color of the uibutton programmatically at a later stage.

In method 2, we have to know the view to cast it as a UITableViewCell and the wrong view will cause us to hit EXE_BAD ACCESS error.

Thanks for the read

Making UILabel Clickable

Many times we normally deal with UITextField and UIButton, this 2 controls allow for user-interaction easily but have not UILabel.

But at times you will still want to use UILabel with an option to allow for user-interaction.

This show code segment allows for you to add a touch event listener to the UILabel. Continue reading “Making UILabel Clickable”