Customize the button in the cell to get the cell's Indexpath

Source: Internet
Author: User


If you are using code to directly add controls (such as Uilabel, UIButton, etc.) to the UITableView cell,

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath

{

Custom code

return cell;

}


After this function, when you click on the cell, you want to know exactly what line is clicked, and then you can get the number of clicks in the following code.

UITableViewCell *cell = (UITableViewCell *) [btn Superview];

Nsindexpath *indexpath = [_mytableview Indexpathforcell:cell];

NSLog (@"Indexpath is =%i", indexpath.row);


Note: BTN is a button,_mytableview that you add to the cell by way of code, which is an associative variable of uitableview.

If you are creating a new, xib Way to create an inherited UITableViewCell. Xib (for example, shopcell.xib) file to add the way to the original UITableView cell, Using this method is not to get the number of rows that are clicked on the cell, that is, you do not know where the clicked cell is the first line.

You can also use the code above, but modify it a little bit:

UITableViewCell *cell = (UITableViewCell *) [[[Btn Superview] superview] superview];

Nsindexpath *indexpath = [_mytableview Indexpathforcell:cell];

NSLog (@"Indexpath is =%i", indexpath.row);

Explanation: [btn Superview] in the first code is the contentviewof Shopcell, and the second superview is the cellof Shopcell itself, The third superview is the cellof UITableView, take care not to confuse it.

Know the row and then use row to determine which row is the cell

Nsindexpath *index1 = [Nsindexpath indexpathforitem:row insection:0];

UITableViewCell *cell1 = [_mytableview cellforrowatindexpath:index1];

Customize the button in the cell to get the cell's Indexpath

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.