【发布时间】:2016-12-12 07:26:10
【问题描述】:
请查看我的自定义 UITableview 单元格的代码,我遇到了这个问题,因为 tableview 委托确实没有调用 select 方法。
我的代码在这里:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(self.cellIdentifier, forIndexPath: indexPath) as! DpMarginRptTableViewCell
cell.selectionStyle = .None
if dataSource.count > 1
{
let dpMarginModel : DPSRDPDetailModel = self.dataSource.objectAtIndex(indexPath.row) as! DPSRDPDetailModel
cell.scriptLabel.text = "\(dpMarginModel.scripCode)"
cell.dpQtyLabel.text = "\(dpMarginModel.dpBalanceAC)"
cell.haricutLabel.text = String(format: "%.2f",Float(dpMarginModel.valueAfterHairCut)/100) //"\(dpMarginModel.valueAfterHairCut)"
cell.transferTextfield.tag = indexPath.row
cell.transferTextfield.text = "\(dpMarginModel.transferText)"
cell.transferTextfield.delegate = self
cell.transferTextfield.layer.borderWidth = 1.0
cell.transferTextfield.layer.borderColor = UIColor.blackColor().CGColor
cell.radioButton1.tag = indexPath.row
cell.radioButton1.addTarget(self, action: #selector(DpMarginView.buttonTapped(_:)), forControlEvents: .TouchUpInside)
cell.radioButton1.setTitle(String.iconWithName(.radioUnchecked), forState: UIControlState.Normal)
cell.radioButton1.setTitle(String.iconWithName(.radioChecked), forState: UIControlState.Selected)
}
}
CustomTableViewCell 类代码
class CustomTableViewCell : UITableViewCell {
var screenWidth:CGFloat!
var screenHeight:CGFloat!
var radioButton1 : UIButton!
var scriptLabel : UILabel!
var dpQtyLabel:UILabel!
var haricutLabel:UILabel!
var transferTextfield: UITextField!
override init(style: UITableViewCellStyle, reuseIdentifier: String?)
{
super.init(style: style, reuseIdentifier: reuseIdentifier)
let screenSize:CGRect = UIScreen.mainScreen().bounds
self.screenWidth = screenSize.width
self.screenHeight = screenSize.height
let boldFont = UIFont(name: "Helvetica Neue", size: 11)?.fontDescriptor().fontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits.TraitBold)
radioButton1 = UIButton(frame: CGRectMake(0, 5, 30, 40))
radioButton1.tag = 101
radioButton1.titleLabel?.font = UIFont(name: "Icomoon", size: 18)
radioButton1.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
radioButton1.setTitleColor(UIColor.blackColor(), forState: UIControlState.Selected)
self.scriptLabel = UILabel.init(frame: CGRectMake(radioButton1.frame.size.width + 5, 5,(screenWidth/4) , 40))
self.scriptLabel.textAlignment = .Left
self.scriptLabel.font = UIFont(descriptor: boldFont!, size: 11)
self.dpQtyLabel = UILabel.init(frame: CGRectMake(scriptLabel.frame.origin.x + scriptLabel.frame.size.width, 5, (screenWidth/4), 40))
self.dpQtyLabel.textAlignment = .Left
self.dpQtyLabel.font = UIFont(name: "HelveticaNeue", size: 11)
self.haricutLabel = UILabel.init(frame: CGRectMake(dpQtyLabel.frame.origin.x + dpQtyLabel.frame.size.width, 5, (screenWidth/4), 40))
self.haricutLabel.textAlignment = .Left
self.haricutLabel.font = UIFont(name: "HelveticaNeue", size: 11)
self.transferTextfield = UITextField.init(frame: CGRectMake((haricutLabel.frame.origin.x + haricutLabel.frame.size.width)-20, 5, (screenWidth/4) - 20, 40))
self.transferTextfield.textAlignment = .Center
self.transferTextfield.font = UIFont(name: "HelveticaNeue", size: 11)
self.transferTextfield.keyboardType = .NumberPad
self.contentView.addSubview(self.radioButton1)
self.contentView.addSubview(self.scriptLabel)
self.contentView.addSubview(self.dpQtyLabel)
self.contentView.addSubview(self.haricutLabel)
self.contentView.addSubview(self.transferTextfield)
self.contentView.backgroundColor = UIColor.clearColor()
}
}
我想在自定义表格视图单元格中添加自定义按钮,一次只能快速选择一个单元格按钮,有人可以帮我吗?
谢谢
【问题讨论】:
-
你能贴一张你的单元格的截图吗?您的问题是
didSelect没有被调用还是单元格内的多个按钮被选中? -
如果您的问题是由于 didselect 方法没有被调用,那么请确保您的代理符合要求,并从故事板检查您的选择在 TableView 中设置为“单选”。跨度>
-
@Nisha 让它也包含在您填充的 tableview 屏幕截图中。
-
谢谢大家,我得到了解决方案。我获取了uitableview的所有可见单元格并取消选择了所有可见单元格。
标签: ios objective-c swift uitableview radio-button