본문 바로가기

전체 글

(46)
[Swift] dequeueReusableCell 테이블 뷰에서 셀을 추가할때 사용하는 함수. 다만 메모리 절약을 위해 스크롤 등으로 화면이 옮겨질때, 이전에 사용되었던 셀이 재사용된다. 그런대 여기서 문제 하나가 재사용되는 셀을 제대로 초기화를 해주지 않는다면 화면이 옮겨지고 난 이후에 버튼등이 제대로 표현되지 않는다. 따라서 셀 내부에 버튼 등 이벤트들은 따로 데이터를 저장해 줄 필요가 있고, 그 데이터를 활용하여 항상 초기화를 해주어야한다. //Table View guard let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCellCharacteristic", for: indexPath) as? CharacteristicTableViewCell else { return UITab..
[Swift] 함수(Function)? 함수 - 아래 func doSomething(parameters) -> ReturnType { print("Test1") return ReturnType } 메서드 - 아래 class TempClass { func doSomething() { print("test2") } } 1. Closure Closure Named Closure(함수) Unnamed Closure(클로저)
[Swift] contentView Declaration var contentView: UIView { get } Discussion The content view of a UITableViewCell object is the default superview for content that the cell displays. If you want to customize cells by simply adding additional views, you should add them to the content view so they position appropriately as the cell transitions in to and out of editing mode. 셀에다가 무언가를 구현할 때, 최상단부터 최하단까지 잘 이어줘야함... 안그러면...
[Swift] isUserInteractionEnabled 확장 테이블 뷰에서 처음 확장때는 버튼이 눌리질 않고 다음 확장때부터는 정상적으로 눌린다. 스탠다드 타입의 테이블뷰에서는 버튼이 동작이 안될 것이다. 그래서 찾아봤더니 아래와 같이 설정해주니 버튼이 정상 동작한다. cell.contentView.isUserInteractionEnabled = false 유저의 이벤트가 event queue로부터 무시되고 삭제됐는지를 판단하는 boolean 값 만약 isUserInteractionEnabled == false를 하면 view를 위한 touch, press, keyboard 그리고 focus event는 event queue에서 무시되고 삭제된다. 반대로 isUserInteractionEnabled == true를 하게 된다면 event는 정상적으로 뷰에 전..
[Swift] NotificationCenter 데이터를 보내는 방법 여러가지가 있지만 아래가 가장 간단했다. 특히 pop up 을 띄울 경우, 이전 뷰컨으로 보낼때 유용했다. //button section NotificationCenter.default.post(name: .writeCharacteristic, object: self.savedTextField) //initial section(viewDidLoad) NotificationCenter.default.addObserver(self, selector: #selector(writeCharacteristic), name: .writeCharacteristic, object: nil) //received data section @objc func writeCharacteristic(_ noti..
[Swift] Substring 만약 text를 자를 일이 있다면 아래와같이 코드블럭 추가해 주는게 편하다. extension String { func substring(from: Int, to: Int) -> String { guard from = 0, to - from >= 0 else { return "" } // Index 값 획득 let startIndex = index(self.startIndex, offsetBy: from) let endIndex = index(self.startIndex, offsetBy: to + 1) // '+1'이 있는 이유: endIndex는 문자열의 마지막 그 다음을 가리키기 때문 // 파싱 return String(self[startIndex ..< endIndex]) ..
[Swift] characteristic read, write //read devicePeripheral.readValue(for: characteristic) //write var parameter = NSInteger(0x31) let data = Data(bytes: &parameter, count: 1) devicePeripheral.writeValue(data, for: characteristic, type: .withResponse) 연결된 peripheral 의 characteristic 은 배열에 따로 저장해 두자.
[Swift] contentView - expandable tableView 왜인지 정확하게 파악은 안되지만 확장 테이블 뷰 사용시 아래 코드중 위의 코드에서는 버튼이 오동작했고, 아래 코드에서는 버튼이 정상 동작하였다. 커스텀하게 사용할때 뭔가 다른 차이점이 있는 것 같기는 한데 아직 파악이 어렵다. addSubview(tableButton) contentView.addSubview(tableButton)