我有一个tableView和tableViewCell。我创建了MenuController来进行setTableView编辑。但似乎失败了。但我使用导航按钮添加“testFunc”动作。它可以改变mutiselected编辑。我怎么想念它呢?我已经添加了tableView的子视图。我还遵循UITableViewDataSource,UITableViewDelegate。
class ViewController: UIViewController,DelegateA, UITableViewDataSource, UITableViewDelegate{
lazy var tableView:UITableView = { ()->UITableView in
let ui:UITableView = UITableView()
ui.delegate = self
ui.dataSource = self
ui.separatorStyle = .none
ui.backgroundColor = defaultBackgroundColor
return ui
}()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.allowsMultipleSelectionDuringEditing = true
}
func testFunc() {
print("123") //it print "123" successful
//not working
if(self.tableView.isEditing == false) {
self.tableView.setEditing(true, animated:true)
}
else {
self.tableView.setEditing(false, animated:true)
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//..........
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// .........
}
}
这是自定义tableViewCell的标签
class UITalkContent:UILabel {
var delegate:DelegateA?
override init(frame: CGRect) {
super.init(frame:frame)
translatesAutoresizingMaskIntoConstraints = false
numberOfLines = 0
isUserInteractionEnabled = true
addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(gestureLongPress)))
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func drawText(in rect: CGRect) {
let insets: UIEdgeInsets = UIEdgeInsets(top: defaultContentPadding, left: defaultPadding, bottom: defaultContentPadding, right: defaultPadding)
super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))
}
func gestureLongPress(sender:UILongPressGestureRecognizer) {
if (sender.state == .ended) {
return
}
becomeFirstResponder()
let menu = UIMenuController.shared
menu.menuItems = [
UIMenuItem(title: localString(string: "FORWARD"), action: #selector(forward)),
]
menu.setTargetRect(bounds, in: self)
menu.setMenuVisible(true, animated: true)
}
func forward(menu :UIMenuController ) {
print("321") //print "321" successfully
self.delegate = ViewController()
self.delegate?.testFunc()
}
override var canBecomeFirstResponder: Bool{
return true
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if (action == #selector(UIResponderStandardEditActions.copy(_:)) || action == #selector(forward)) {
return true
}
return false
}
override func copy(_ sender: Any?) {
UIPasteboard.general.string = text
}
}
protocol DelegateA {
func testFunc()
}
class ContentTableViewCell: UITableViewCell {
var labelContent:UILabel = { ()->UILabel in
let ui:UILabel = UITalkContent()
ui.textColor = UIColor(red:0.20, green:0.20, blue:0.20, alpha:1.00)
ui.backgroundColor = UIColor.white
ui.font = defaultTextFont
ui.numberOfLines = 0
ui.lineBreakMode = NSLineBreakMode.byCharWrapping
ui.layer.cornerRadius = defaultButtonRadius
ui.layer.masksToBounds = true
ui.isHidden = false
return ui
}()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
loadContent()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func layoutSubviews() {
super.layoutSubviews()
loadVFL()
}
func loadContent() {
contentView.addSubview(labelContent)
}
func loadVFL() {
labelContent.frame = CGRect(x: 0, y: 0, width: contentWidth, height: CGFloat.greatestFiniteMagnitude)
labelContent.sizeToFit()
}
}
我想要点击“向前”菜单来显示这样的视图
谢谢。
Hello here is the image what i want.please help me thanks.
答案 0 :(得分:0)
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
dataArray=[[NSMutableArray alloc]init];
UIButton *btn = [[UIButton alloc]init];
btn.backgroundColor = [UIColor blueColor];
[btn setTitle:@"Click Me" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(didTapBringCheckBoxBtn) forControlEvents:UIControlEventTouchUpInside];
btn.frame = CGRectMake(0, 0, self.view.frame.size.width, 100);
[self.view addSubview:btn];
[dataArray addObject:@"Apple"];
[dataArray addObject:@"Mango"];
[dataArray addObject:@"Papaya"];
[dataArray addObject:@"Guava"];
[dataArray addObject:@"Pineapple"];
[dataArray addObject:@"Strawberry"];
[dataArray addObject:@"Banana"];
[dataArray addObject:@"Grapes"];
[dataArray addObject:@"Pomegranate"];
[dataArray addObject:@"Green Tea"];
[dataArray addObject:@"Raisin"];
self.mTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, btn.frame.size.height, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
self.mTableView.delegate = self;
self.mTableView.dataSource = self;
[self.view addSubview:self.mTableView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - Check Box Button Action
- (void)didTapBringCheckBoxBtn
{
[self.mTableView setEditing:YES animated:YES];
}
#pragma mark - UITableView DataSource Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dataArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
return cell;
}
#pragma mark - UITableView Delegate Methods
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 3;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"user selected %@",[dataArray objectAtIndex:indexPath.row]);
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"user de-selected %@",[dataArray objectAtIndex:indexPath.row]);
}
@end