"Go" about UITableViewCell's Accessorytype property

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/kmyhy/article/details/6442351

If used, for example:

[CPP]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. Cell.accessorytype = Uitableviewcellaccessorynone; //cell doesn't have any style.
    2. Cell.accessorytype = Uitableviewcellaccessorydisclosureindicator; There is a small arrow on the right side of the//cell, more than 10 pixels from the right.
    3. Cell.accessorytype = Uitableviewcellaccessorydetaildisclosurebutton; //cell to the right there is a blue round button;
    4. Cell.accessorytype = Uitableviewcellaccessorycheckmark; //cell to the right of the shape is a checkmark;

For UITableViewCell, there are 4 kinds of values for the Accessorytype attribute:

Uitableviewcellaccessorynone,

Uitableviewcellaccessorydisclosureindicator,

Uitableviewcellaccessorydetaildisclosurebutton,

Uitableviewcellaccessorycheckmark

4 styles for UITableViewCell additional buttons are displayed:

No.

In addition, if you want to use other styles of the custom attachment button, you must use the UITableView Accessoryview property. For example, we want to customize a

The attachment button, you can do this:

UIButton *button;

if (Iseditableornot) {

UIImage *image= [UIImage imagenamed:@ "Delete.png"];

button = [UIButton buttonwithtype:uibuttontypecustom];

CGRect frame = CGRectMake (0.0, 0.0, image.size.width, image.size.height);

Button.frame = frame;

[Button SetBackgroundImage:imageforState:UIControlStateNormal];

button.backgroundcolor= [Uicolor Clearcolor];

cell.accessoryview= button;

}else {

button = [UIButton buttonwithtype:uibuttontypecustom];

button.backgroundcolor= [Uicolor Clearcolor];

cell.accessoryview= button;

}

This way, when the Iseditableornot variable is YES, the following view is displayed:

However, there is still a problem, and the Attachment button event is not available. That is, an event cannot be passed to the Uitableviewdelegate Accessorybuttontappedforrowwithindexpath method.

Perhaps you would say that we can add a target to UIButton.

OK, let's see if we can. In the code above, add:

[Button addtarget:self action: @selector (btnclicked:event:) forcontrolevents:uicontroleventtouchupinside];

Then implement the Btnclicked method and simply add something to it.

Clicking on the code in each uibutton,btnclicked method is called! Everything is so perfect.

The problem is that each UITableViewCell has an attachment button, and when you click on a button, we don't know which button has clicked! Because the Addtarget method does not allow us to pass a parameter that distinguishes the buttons, such as Indexpath.row or something else. The AddTarget method allows a maximum of two parameters to be passed: Target and event, and we did. But both of these parameters have their own purpose, and target points to the event delegate Object--Here we pass the self-uiviewcontroller instance to it, which points to the event that occurred, such as a single touch or multiple touches. We need extra parameters to pass the UITableViewCell or row index that the button belongs to, but unfortunately we can't do it by relying only on the cocoa framework.

But we can use the event parameter to determine in the Btnclicked method which cell the event occurred on UITableView. Because UITableView has a critical approach indexpathforrowatpoint, you can return the Indexpath of the cell to which the touch occurred, depending on where the touch occurred. And with the event object, we can also get the position of each touch in the view:

Check the position of the user when the button is clicked and forward the event to the corresponding accessorytapped event

-(void) btnclicked: (ID) senderevent: (ID) event

{

Nsset *touches =[event alltouches];

Uitouch *touch =[touches Anyobject];

Cgpointcurrenttouchposition = [Touch LocationInView:self.tableView];

Nsindexpath *indexpath= [Self.tableview indexpathforrowatpoint:currenttouchposition];

if (indexpath!= nil)

{

[Self TableView:self.tableView accessorybuttontappedforrowwithindexpath:indexpath];

}

}

This way, the Accessorybuttontappedforrowwithindexpath method of the UITableView is triggered and a indexpath parameter is obtained. With this indexpath parameter, we can distinguish between which of the many buttons the attachment button has a touch event:

-(void) TableView: (uitableview*) TableView Accessorybuttontappedforrowwithindexpath: (nsindexpath*) indexpath{

int* idx= Indexpath.row;

To add your own logic here.

??

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.