我目前有一个UITableView,其中填充了一个单独的笔尖中的自定义UITableViewCell.在单元格中,有两个按钮连接到自定义单元类中的操作.当我单击其中一个按钮时,我可以调用正确的方法,但我需要知道按下哪一行按钮.每个按钮的标签属性为0.我不需要知道整个单元格的时间选择,只是当按下一个特定的按钮时,所以我需要知道它是哪一行,所以我可以更新正确的对象.
我有一个很大的问题.我正在尝试在每个UITableViewCell中创建一个最喜欢的按钮UITableView.这非常有效,我现在按下时会执行一个动作和选择器.
accessory = [UIButton buttonWithType:UIButtonTypeCustom];
[accessory setImage:[UIImage imageNamed:@"star.png"] forState:UIControlStateNormal];
accessory.frame = CGRectMake(0, 0, 15, 15);
accessory.userInteractionEnabled = YES;
[accessory addTarget:self action:@selector(didTapStar) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = accessory;
Run Code Online (Sandbox Code Playgroud)
和选择器:
- (void) didTapStar {
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:/* indexPath? */];
accessory = [UIButton buttonWithType:UIButtonTypeCustom];
[accessory setImage:[UIImage imageNamed:@"stared.png"] forState:UIControlStateNormal];
accessory.frame = CGRectMake(0, 0, 26, 26);
accessory.userInteractionEnabled = YES;
[accessory addTarget:self action:@selector(didTapStar) forControlEvents:UIControlEventTouchDown];
newCell.accessoryView = accessory;
}
Run Code Online (Sandbox Code Playgroud)
现在,问题在于:我想知道按下的配件属于哪一行.我怎样才能做到这一点?
谢谢 :)
我有一个UITableview多个细胞.在每个单元格上,我根据一些数组计数动态添加按钮.所以,当我点击一个按钮时,我就可以获得按钮的tag值.但是如何获得indexPath那个细胞呢?
这是我的代码-cellForRowAtIndexPath:
UIView *view=(UIView*)[cell.contentView viewWithTag:indexPath.row+444];
UIImageView *img=(UIImageView*)[cell.contentView viewWithTag:indexPath.row+999];
img.image=[UIImage imageNamed:@"BHCS_empty.png"];
if(integer!=50)
{
NSInteger y_axis=0;
NSArray *Arr=[tableSubCategoryArr objectAtIndex:indexPath.row];
img.image=[UIImage imageNamed:@"BHCS_selected.png"];
view.Frame= CGRectMake(0,50,281, integer-50);
for (int i=0; i<[Arr count]; i++)
{
NSLog(@"arr %@",[Arr objectAtIndex:i]);
UIButton *Btn=[UIButton buttonWithType: UIButtonTypeCustom];
Btn.frame=CGRectMake(0, y_axis, 281, 44);
[Btn setImage:[UIImage imageNamed:@"BHCS_panel.png"] forState:UIControlStateNormal];
[Btn addTarget:self action:@selector(subCategoryBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[Btn setTag:i+100];
[view addSubview:Btn];
UILabel *nameLbl=[[UILabel alloc]initWithFrame:CGRectMake(20, y_axis,248, 44)];
nameLbl.text = [[Arr objectAtIndex:i]objectForKey:@"SubCategoryName"];
nameLbl.textColor = [UIColor whiteColor];
nameLbl.backgroundColor=[UIColor clearColor];
panelTableView.separatorColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"BHCS_panel_div1.png"]]; …Run Code Online (Sandbox Code Playgroud) 我加入UIButton了UITableViewCells.我有用户单击按钮时,我们已获得索引路径以使用来自的值NSMutableArray.我用下面的方法来获取当前的信息IndexPath,
[getMeButton addTarget:self action:@selector(resendTheErrorMessage:) forControlEvents:UIControlEventTouchUpInside];
-(void) resendTheErrorMessage:(id)sender
{
NSLog(@"SEnder: %@", sender);
//NSLog(@"Index Path : %@", indexpath);
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我传递当前的indexpath UIButton's@selector.提前致谢.
编辑:
这是我得到的输出 NSLog()
<UIButton: 0x864d420; frame = (225 31; 95 16); opaque = NO; tag = 105; layer = <CALayer: 0x864d570>>
Run Code Online (Sandbox Code Playgroud) 到目前为止,我只需要实现具有预定义设计(正常,副标题等)的原型单元,这不是问题.
现在我需要实现原型单元,其中包含一些控件,如分段开关,开关或任何其他.问题是我无法找到触发的动作是如何实现的以及它们与控件的关系.此外,我还没有找到任何关于如何在单个UITableViewController中实现不同原型单元的示例.
我知道这是一个普遍的问题,但我会在这里感谢一些指点.也许有人知道一些文档,教程等等.好吧,任何帮助都可以,
事先提醒.
我创建了一个具有自定义UITableViewCell的TableView.每个tableview行都有一个按钮.现在我想知道单击按钮时的行号,以便我知道单击了哪个行按钮.我尝试过在堆栈上找到的一些东西,但没有任何工作.
我试过这段代码 - :
-(void)button1Tapped:(id)sender
{
UIButton *senderButton = (UIButton *)sender;
UITableViewCell *buttonCell = (UITableViewCell *)[senderButton superview];
UITableView* table = (UITableView *)[buttonCell superview];
NSIndexPath* pathOfTheCell = [table indexPathForCell:buttonCell];
NSInteger rowOfTheCell = [pathOfTheCell row];
NSLog(@"rowofthecell %d", rowOfTheCell);
}
Run Code Online (Sandbox Code Playgroud)
但这也行不通.
谢谢你的协助.
我在UITableViewCell中有一个UIButton,自iOS4以来一直正常工作,现在自iOS7更新后它就不再有效了.它基本上是一个空盒子的图像.当用户单击图像(UIButton)时,图像将变为复选框.我没有使用XIB.有人有什么建议吗?提前致谢.
(我已经尝试了contentView.userInteractionEnabled = NO;,[cell bringSubviewToFront:button]但这不起作用)
这是一些相关的代码:
- (UITableViewCell *)taskCell:(NSIndexPath *)indexPath table:(UITableView *)localTableView managed:(NSManagedObject *)managedTask dateFormat:(NSDateFormatter *)localDateFormatter{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [localTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
// Checkbox
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(-4.0f, -3.0f, 48.0f, 48.0f)];
button.tag=kCellButtonViewTag;
button.adjustsImageWhenHighlighted=NO;
[button setImage:[UIImage imageNamed:@"uncheckedPriorityNone.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(toggleCheckedMode:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
[button release];
}
}
- (IBAction)toggleCheckedMode:(id)sender{
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
UITableViewCell *clickedCell = (UITableViewCell …Run Code Online (Sandbox Code Playgroud) 我有一个UITableView,其中我添加了一个UIButton作为每个单元格的附件视图.请注意,我将按钮的标记设置为当前行以供将来使用.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cellButton = [UIButton buttonWithType:UIButtonTypeCustom];
cellButton.frame = CGRectMake(0, 0, 30, 30);
[cellButton setBackgroundImage:[UIImage imageNamed:@"cellButton.png"] forState:UIControlStateNormal];
[cellButton addTarget:self action:@selector(cellButtonAction:) forControlEvents:UIControlEventTouchUpInside];
cellButton.tag = indexPath.row; // <= Will use this in the next method
cell.accessoryView = cellButton;
}
//Load cell with row based data
return cell;
}
Run Code Online (Sandbox Code Playgroud)
现在当点击其中一个按钮时,我需要对单元格进行更改.所以我实现了cellButtonAction,我使用标签来取回单元格:
-(void)editCommentButtonAction:(id)sender
{
UIButton *button …Run Code Online (Sandbox Code Playgroud) 我UITableView在我的应用程序中有UITableViewCell一个包含a的自定义UITextField.我想要做的是从该行中选择特定文本字段时获取行的正确索引.不幸的是,如果我实际点击该行,我只能获得行的正确索引,而不是当我选择文本字段本身时.我正在实现<UITextFieldDelegate>,当我选择一个特定的时候UITextField,我调用该方法:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
int rowTest = [_table indexPathForSelectedRow].row;
int rowTest1 = [_cell.tireCodeField tag];
NSLog(@"the current row is: %d", rowTest1);
NSLog(@"and this row is: %d", rowTest);
return YES;
}
Run Code Online (Sandbox Code Playgroud)
问题是,我得到的行的值来自方法:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
...
}
Run Code Online (Sandbox Code Playgroud)
得到的.就好像UITextField它所在表中的行和行之间存在脱节.有没有办法让我选择一个特定的textField,然后得到
有没有办法通过选择行中的索引来获取行的索引UITextField,而不是选择行本身?
在此先感谢所有回复的人.
我有一个UITableView,我已经为其创建了一个自定义的UITableViewCell.tableview中的每一行都有一个按钮.我想知道单击按钮时的节号,以便我知道单击了哪个部分按钮.我已经尝试过在堆栈上找到的一些东西,但没有任何工作.
UIButton *b = sender;
NSIndexPath *path = [NSIndexPath indexPathForRow:b.tag inSection:0];
NSLog(@"Row %d - Section : %d", path.row, path.section);
Run Code Online (Sandbox Code Playgroud) uitableview ×10
ios ×7
iphone ×4
uibutton ×4
objective-c ×2
accessory ×1
ios4 ×1
ios7 ×1
parameters ×1
prototype ×1
row ×1
uitextfield ×1
xcode ×1