IOS gets the cell through the button and iosbutton gets the cell
When using tableview, sometimes we need to add buttons and labels to the cell to add a function, in addition, you often need to know the value of the label memory in the cell where the button is located in the button method.
In general, we can use tags, but when there are many rows in the table, it is not that convenient to set tags. Here I will introduce another method.
We know that every view in IOS has a parent view. Can we use this method to obtain the cell?
After some search tests, we found that the superview method can find the cell where the button is located.
Write this method to the button.
-(void)btn_Onclick:(id)sender{ UIButton *btn = (UIButton *)sender; NSLog(@" [btn superview] = %@ ",[[btn superview]class]); NSLog(@" [[[btn superview]superview]class] = %@",[[[btn superview]superview]class]);}
The output result is as follows:
2014-09-10 19:09:42.203 superview[4720:60b] [btn superview] = UITableViewCellScrollView 2014-09-10 19:09:42.646 superview[4720:60b] [[[btn superview]superview]class] = UITableViewCell
It indicates that there is no problem. However, after my tests, I found that the class that I wrote inherits from uitableviewcell (here I am SongTableViewCell) needs to be slightly modified.
NSLog(@" [btn superview] = %@ ",[[btn superview]class]); NSLog(@" [[[btn superview]superview]class] = %@",[[[btn superview]superview]class]); NSLog(@" [[[btn superview]superview]superview]class] = %@",[[[[btn superview]superview]superview] class]);
The output result is as follows:
2014-09-10 18:54:50.036 Music_Player[4451:60b] [btn superview] = UITableViewCellContentView 2014-09-10 18:54:50.036 Music_Player[4451:60b] [[[btn superview]superview]class] = UITableViewCellScrollView2014-09-10 18:54:50.036 Music_Player[4451:60b] [[[btn superview]superview]superview]class] = SongTableViewCell
PS: For the viewwithtag method, this method cannot find the control with the tag as 0. Even if the tag of the button is 0, it cannot be found. It is assumed that it is the same as the default space of the system.
Every cell in iOS tableview has a button. How can I know that the button has been clicked?
1. You can solve the problem by creating a new class that inherits UIButton and adds an int attribute. Then, set the cell to make the int equal to indexPath. row (it is troublesome to customize the button or cell)
2. Set a tag value for each button to indexPath. row + fixed constant (the implementation of simple code is not robust and the logic is complicated and difficult to handle)
3. Customize the cell and then use the btnClick event to process the dependencies and context when generating the cell in the cell. If there are multiple cell types abstracted in the factory mode, use the Protocol to write the interface and pay attention to the context when referencing the context. distinguish weak and strong (avoid circular reference) if you are not familiar with the protocol, you can use the notification center to call back ViewController (this method is highly required for developers, but it is recommended that the code be robust, reusable, and highly encapsulated)
4. Use block encapsulation to process events. Similar Method 1. Rewrite btn (high memory usage is not recommended)
There are other ways to explain in detail how to develop more brains. According to the actual situation, IOS development is relatively free to implement a function. There are many ways.
How to obtain the currently pressed button for iOS development
Set a tag for each button, and then perform the following operations in your custom method:
-(Void) _ btnPressed :( id) sender
{
UIButton * btn = (UIButton *) sender;
Switch (btn. tag ){
Case xxx:
// Do your own work .....
Break;
...
Default: break;
}
}
//******************************//
Your writing method is very beautiful. You should write it like this when there are many buttons,
When the button has its own meaning, we can use an enum to indicate the tag, for example:
Typdef MYBUTTON {
TEST_BTN = 0,
REAL_BTN,
XX_BTN,
YY_BTN
} M_BTN;
[Btn settag: TEST_BTN];
Switch M_BTN
Case: XX_BTN
...
...
....