In iOS development, a common mode is mark configuration, Mark & Configure
Steps are:
-
Mark, mark
-
Status restored, reset
-
The configuration, configure
This article through three examples, to illustrate
The first two examples are the selected state
Example 1: button click
Three buttons, select one and deselect the other two
routine
Operation specified, reset remaining
The maintenance cost of the code is high
class ViewController: UIViewController {
@IBOutlet weak var lhs: StateBtn!
@IBOutlet weak var mid: StateBtn!
@IBOutlet weak var rhs: StateBtn!
func setup(){
lhs.addTarget(self, action: #selector(btnOne), for: .touchUpInside)
mid.addTarget(self, action: #selector(btnTwo), for: .touchUpInside)
rhs.addTarget(self, action: #selector(btnThree), for: .touchUpInside)
}
@objc
func btnOne(){
lhs.beSelected = true
mid.beSelected = false
rhs.beSelected = false
}
@objc
func btnTwo(){
lhs.beSelected = false
mid.beSelected = true
rhs.beSelected = false
}
@objc
func btnThree(){
lhs.beSelected = false
mid.beSelected = false
rhs.beSelected = true
}
}
Copy the code
After the model
Perform this operation after a unified reset
Code maintenance costs have been reduced
@objc
func btnOne(){
resetAll()
lhs.beSelected = true
}
@objc
func btnTwo(){
resetAll()
mid.beSelected = true
}
@objc
func btnThree(){
resetAll()
rhs.beSelected = true
}
func resetAll(){
lhs.beSelected = false
mid.beSelected = false
rhs.beSelected = false
}
Copy the code
Example 2: Table View
A little bit more obvious
Select one of the three cells and deselect the other two cells
General practice:
Func tableView(_ tableView: UITableView, didSelectRowAt indexPath: indexPath),
After the selection, fish out the previous cell, fish out the selected cell,
Change the status of two cells
Maybe the problem is, take the first cell out and pop it
Because maybe the previous cell is not on screen and has entered the reuse pool, no more
Now, if I select this cell, it must be on the screen, it must exist
class ViewController{ @IBOutlet weak var table: UITableView! func setup(){ table.register(UINib(nibName: TableCell.k, bundle: nil), forCellReuseIdentifier: TableCell.k) table.delegate = self table.dataSource = self table.rowHeight = 60 table.reloadData() } } extension ViewController: UITableViewDelegate{ func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {if let last = lastSelectedItem{guard last! = indexPath.row else{ return } if let cell = tableView.cellForRow(at: IndexPath(row: last, section: 0)) as? TableCell{cell.beselected = false}} if let cell = tableView.cellForRow(at: indexPath) as? TableCell{ cell.beSelected = true } lastSelectedItem = indexPath.row } } extension ViewController: UITableViewDataSource{ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 3 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let item = tableView.dequeueReusableCell(withIdentifier: TableCell.k, for: indexPath) return item } }Copy the code
After the model
Previously, the row selected last time was logged. Now, the row currently selected is logged
Simpler logic
In two stages,
-
Select the agent, record, and refresh the list
-
To refresh the list, the configuration method is called,
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
You can select states based on the data
There are no pitfalls of the previous step
To enhance the effect, use deep Diff
class ViewController: UIViewController {
@IBOutlet weak var table: UITableView!
var selectedItem: Int? = nil
}
extension ViewController: UITableViewDelegate{
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedItem = indexPath.row
tableView.reloadData()
}
}
extension ViewController: UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item = tableView.dequeueReusableCell(withIdentifier: TableCell.k, for: indexPath)
if let cel = item as? TableCell{
if let row = selectedItem, row == indexPath.row{
cel.beSelected = true
}
else{
cel.beSelected = false
}
}
return item
}
}
Copy the code
Example 3,MBProgressHUD
Bar bar
- (void)barDeterminateExample { MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES]; / / marker hud. Mode = MBProgressHUDModeDeterminateHorizontalBar; }Copy the code
Tag after trigger
- (void)setMode:(MBProgressHUDMode)mode {
if (mode != _mode) {
_mode = mode;
[self updateIndicators];
}
}
Copy the code
Go to update
- (void) updateIndicators {the if (mode = = MBProgressHUDModeDeterminateHorizontalBar) {/ / reset the original [indicator removeFromSuperview]; // Use new indicator = [[MBBarProgressView alloc] init]; [self.bezelView addSubview:indicator]; } / / second mark [self setNeedsUpdateConstraints]; }Copy the code
Rearrange the layout
#pragma mark - Layout
- (void)updateConstraints {
// ...
}
Copy the code