[英]how can get which uibutton when know indexPath.row of uitableview in ios
我有一個uitableview,它在每行中都有2個uibutton(button1和button2)。 現在我知道indexPath.row(例如:indexPath.row = 2)。 當我有uitableview的IndexPath.row時如何獲得button1? 請給我任何解決此問題的建議。 提前致謝。
我使用以下代碼:
-(void)UpdateBtn:(NSNotification *)notification {
//Update your button image code here
shotIDPass =[[NSUserDefaults standardUserDefaults]valueForKey :@"videoIDstring"];
NSLog(@"%@",shotIDPass);
for (int i =0; i < [shotIDArray count]; i++) {
if([[shotIDArray objectAtIndex:i] isEqualToString:shotIDPass])
{
NSLog(@"IndexPath of row %@",i); // get indexPath.row at here
}
}
}
您需要創建一個自定義單元格類。 然后使用tableview委托方法。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (nil == cell) {
cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;
}
完成后,您可以在按鈕上添加操作。
您可以在xib或情節提要中執行此操作,以與IBOutlet鏈接或在MyCustomCell.m中以編程方式鏈接
只需將IBOutlet設置為按鈕,然后在方法中分配像這樣的執行選擇器即可
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell.button1 addTarget:self action:@selector(button1:) forControlEvents:UIControlEventTouchUPInside];
[cell.button2 addTarget:self action:@selector(button2:) forControlEvents:UIControlEventTouchUPInside];
return cell;
}
然后像這樣實現按鈕點擊的方法
-(IBAction)button1:(UIButton *)btn
{
//Your Implementation
}
和第二個按鈕一樣
在這里,我解釋了如何設置按鈕標簽並找到單擊該按鈕的index.row
-(IBAction)clickEvent:(UIButton *)sender;
-(void)viewDidload{
NSMutableArray *arrayCellCollection = [[NSMutableArray alloc] init];
int tagCount=1;
for (int i =0; i < [shotIDArray count]; i++){
AddPropertyCell *cell = [[AddPropertyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifiour"];
cell.button1.tag=tagCount;
[cell.button1 addTarget:self action:@selector(clickEvent:)forControlEvents:UIControlEventTouchUpInside];
tagCount++;
cell.button2.tag=tagCount;
[cell.button2 addTarget:self action:@selector(clickEvent:)forControlEvents:UIControlEventTouchUpInside];
tagCount++;
[arrayCellCollection addObject:cell];
}}
-(IBAction)clickEvent:(UIButton *)sender
{int getIndexRow =0;
int a = sender.tag %2;
if (a==0)
{
getIndexRow=((a/2)-1);
}
else
{
getIndexRow=(a/2);
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
return [arrayCellCollection objectAtIndex:indexPath.row];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.