我有一个UITableView5 UITableViewCells.每个单元格包含一个UIButton设置如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = @"identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
[cell autorelelase];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 5, 40, 20)];
[button addTarget:self action:@selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTag:1];
[cell.contentView addSubview:button];
[button release];
}
UIButton *button = (UIButton *)[cell viewWithTag:1];
[button setTitle:@"Edit" forState:UIControlStateNormal];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:在buttonPressedAction:方法中,我如何知道按下了哪个按钮.我考虑过使用标签,但我不确定这是最好的路线.我希望能够以某种方式标记indexPath到控件上.
- (void)buttonPressedAction:(id)sender
{
UIButton *button = (UIButton …Run Code Online (Sandbox Code Playgroud) 我有一个tableview,其中包含可变数量的单元格,表示与其特定教师相对应的学生.它们是具有按钮的自定义单元格,该按钮触发新VC的segue,提供有关其单元格的学生的详细信息.我的问题是:
swift中用于识别按下哪个按钮的最佳做法是什么?
一旦我知道索引路径,我就可以确定哪个学生的信息需要传递给下一个VC.在下面的帖子中,对于目标C有一个很好的答案,但我不确定如何转换为Swift.任何帮助将非常感激.
我在UITableViewCell中有一个操作按钮,我想检测按钮的按下时间以及ViewController中按下的单元格编号,以便在ViewController.swift中创建音频播放列表.
我已经陷入这个问题一段时间了,我真的很赞赏你的建议.这是代码.
ViewController.swift
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.register(UINib(nibName: "Cell", bundle: nil), forCellReuseIdentifier: "cell")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! Cell
return cell
}
}
Run Code Online (Sandbox Code Playgroud)
Cell.swift
import UIKit
class Cell: UITableViewCell {
@IBOutlet weak var button: UIButton! …Run Code Online (Sandbox Code Playgroud) 我试图在单元格外部的集合视图中更改自定义视图单元格的某些属性,但我无法弄清楚我做错了什么:
在 cellForItem 我有:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PatientProfileCell", for: indexPath) as! PatientQuestionCell
if Auth.auth().currentUser?.uid == userID {
cell.commentButton.setTitle("Edit post", for: .normal)
cell.commentButton.addTarget(self, action: #selector(editPostAction), for: .touchUpInside)
}
} else {
print("Not the current user for collection view")
}
}
Run Code Online (Sandbox Code Playgroud)
在这里一切正常,但是当我触发动作时什么也没有发生。这是功能:
//post actions
func editPostAction() {
print("Edit post Enabled")
let cell = PatientQuestionCell()
cell.questionView.isEditable = true
cell.questionView.isSelectable = true
cell.questionView.isScrollEnabled = true
cell.questionView.becomeFirstResponder()
}
Run Code Online (Sandbox Code Playgroud)
如何使用此功能更改单元格的属性?
我在tableview单元格上设置了一个按钮,我想知道如何操作它.也就是说,我有兴趣更改按钮的名称标题,并在单击按钮时添加另一个目标(按钮的不同功能).我的想法是需要将它添加到buttonClicked func中,但我不确定如何引用被单击的特定cellForRow.或许可以在cellForRow中使用条件来确定按钮标题是什么?我不太清楚最好的办法是什么.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = guestTableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let button : UIButton = UIButton(type:UIButtonType.custom) as UIButton
button.frame = CGRect(origin: CGPoint(x: 200,y :60), size: CGSize(width: 100, height: 24))
let cellHeight: CGFloat = 44.0
button.center = CGPoint(x: view.bounds.width / (4/3), y: cellHeight / 2.0)
button.setTitleColor(.blue, for: .normal)
button.addTarget(self, action: #selector(buttonClicked), for: UIControlEvents.touchUpInside)
button.setTitle("Add", for: UIControlState.normal)
cell.addSubview(button)
return cell
}
func buttonClicked(sender : UIButton!) {
print("Added!")
}
Run Code Online (Sandbox Code Playgroud) 我已经阅读了类似这样的问题,但这些代码对我没有用,所以请帮助:我有一个表格视图,每个单元格中有一些按钮 - 这个表视图是用户的因素,我添加了按钮到该tableview - 当用户有付费的钱按钮隐藏在那个单元格中的那个因素中,另一个单元格中隐藏另一个因素当用户没有支付付款时没有隐藏 - 这就是我想要检测哪些单元格按钮按下了?!我知道在表视图中我们可以检测到哪个单元格已被按下但是这是不同的,因为用户只需按下该单元格中的按钮不按下所有单元格
由于我的代码太长,我只需将表格视图代码放在这里
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "factorCell", for: indexPath) as! FactorCell
cell.factorTitle.text = factorTitle[indexPath.row]
cell.factorCode.text = factorCodes[indexPath.row]
cell.factorDate.text = factorDate[indexPath.row]
cell.factorHour.text = factorHour[indexPath.row]
cell.factorPrice.text = factorPrice[indexPath.row]
cell.factorCondition.text = factorConditions[indexPath.row]
cell.factorBill.tag = indexPath.row
cell.factorBill.addTarget(self,action:#selector(factorViewController.Bill), for: UIControlEvents.touchUpInside)
cell.factorDetail.tag = indexPath.row
cell.factorDetail.addTarget(self,action:#selector(factorViewController.FactorDetail), for: .touchUpInside)
if factorConditions[indexPath.row] == "?????? ???" {
cell.factorBill.isHidden = true
} else {
cell.factorCondition.textColor = UIColor.red
cell.factorBill.isHidden = false
}
return cell
} …Run Code Online (Sandbox Code Playgroud) 我想实现如果在 tableviewCell 中点击 profileImage 的功能,则转至 detailView。我添加了 tapGesture 但我仍然无法弄清楚如何让 indexPath 传递数据。我试过这样,但应用程序会崩溃。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "detailSegue" {
let next: DetailViewController = segue.destination as! DetailViewController
// pattern1
let indexPath = tableView.indexPath(for: sender as! CustomTableViewCell)
// pattern2
let indexPath = tableView.indexPathForSelectedRow! as NSIndexPath
next.data = datas[indexPath.row]
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?先感谢您!
我有两个UILabels,两个UITapGestureRecognizers在一个UITableViewCell.
cell.Username.tag = indexPath.row
cell.SharedUser.tag = indexPath.row
let tapGestureRecognizer2 = UITapGestureRecognizer(target:self, action:"GoToProfil:")
let tapGestureRecognizer3 = UITapGestureRecognizer(target:self, action:"GoToProfil:")
cell.Username.userInteractionEnabled = true
cell.Username.addGestureRecognizer(tapGestureRecognizer2)
cell.SharedUser.userInteractionEnabled = true
cell.SharedUser.addGestureRecognizer(tapGestureRecognizer3)
func GoToProfil (sender: AnyObject!) {
self.performSegueWithIdentifier("GoToProfilSegue", sender: sender)
}
Run Code Online (Sandbox Code Playgroud)
我正在使用 aSegue来推送另一个UIViewController,并且我正在覆盖该PrepareSegue函数以发送与Sender标签相对应的所需信息。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
let ProfilView = segue.destinationViewController as! Profil
ProfilView.hidesBottomBarWhenPushed = true
ProfilView.title = posts[sender.view!.tag].User?.objectForKey("Name") as? String
ProfilView.User = posts[sender.view!.tag].User
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我想知道UILabel按下了哪个,知道我已经在使用tag.
ios ×7
swift ×6
uitableview ×6
uibutton ×3
swift3 ×2
cell ×1
iphone ×1
nsindexpath ×1
segue ×1