Jam*_*s V 9 iphone xcode objective-c uibutton uitableview
我有一个带有自定义UITableViewCell(cell.h/.m/.xib)的项目,它有2个标签(labelMain/labelSub),上面有2个按钮(buttonName/buttonInfo)链接到2个动作(showName/showInfo).
我想要做的是能够访问我的项目主视图控制器中的2个动作,这样当按下showName时,viewcontroller(不在单元格中)的textfield.text被设置为该特定单元格的labelMain.text.
希望有道理.我的问题是,如果我在cell.m中编写动作(showName),我无法从主视图控制器访问文本字段.另一方面,如果我在viewcontroller中编写动作,我如何知道哪个按钮位于哪个按钮内?
希望这有道理......
Hel*_*miB 16
使用tag可以识别正在点击哪个单元格的按钮.
- (UITableViewCell *)tableView:(UITableView *)tableViews cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//init identifier
if (cell ==nil)
{
//load nib file
}
buttonName.tag = indexPath.row;
[buttonName addTarget:self action:@selector(showName:) forControlEvents:UIControlEventTouchUpInside];
buttonInfo.tag = indexPath.row;
[buttonInfo addTarget:self action:@selector(showInfo:) forControlEvents:UIControlEventTouchUpInside];
}
}
-(void) showName:(UIButton*)button{
int row = button.tag; //you know that which row button is tapped
}
-(void)showInfo:(UIButton*)button{
int row = button.tag;//you know that which row button is tapped
}
Run Code Online (Sandbox Code Playgroud)
如果您需要知道基于行和部分按下了哪个按钮,您可以尝试以下方法.(cellForRowAtIndexPath:方法中)
int tag = (indexPath.row+1)+(indexPath.section*100);
buttonName.tag = tag;
Run Code Online (Sandbox Code Playgroud)
当按钮在
row = 5,section = 0 then tag = 6.
row = 4,section = 3 then tag = 305.
row = 0,section = 11 then tag = 1101.
限制,行不能超过99.并且不要在其他视图中使用正标记.如果你需要使用标签,请尝试使用否定.(-1,-9,-100).
所以从这里开始,你可以计算indexPath的后行和部分.使用这个:
-(NSIndexPath*)getIndexPathFromTag:(NSInteger)tag{
/* To get indexPath from textfeidl tag,
TextField tag set = (indexPath.row +1) + (indexPath.section*100) */
int row =0;
int section =0;
for (int i =100; i<tag; i=i+100) {
section++;
}
row = tag - (section*100);
row-=1;
return [NSIndexPath indexPathForRow:row inSection:section];
}
Run Code Online (Sandbox Code Playgroud)
如何使用 :
-(void)showInfo:(UIButton*)button{
NSIndexPath *indexPath = [self getIndexPathFromTag:button.tag];
int row = indexPath.row;
int section = indexPath.section;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11180 次 |
| 最近记录: |