We will delete
a tableview row entirely, when reopen, it not appear again.
We already have array fru that we read to file ghi.txt.
Copy to above last close bracket.
func doc()->Int{
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] asNSString
let path = documentsPath.stringByAppendingPathComponent("ghi.txt")
var read : String = ""
var t=0
do {
try read = NSString(contentsOfFile: path, encoding: NSUTF8StringEncoding) asString
t = read.characters.count
}
catchlet error asNSError {
// print("ERROR :
reading from file \(fileName) : \(error.localizedDescription)")
}
return t
}
func doc2()->[String]{
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] asNSString
let path = documentsPath.stringByAppendingPathComponent("ghi.txt")
var read2 : String = ""
var q=[String]()
do {
try read2 = NSString(contentsOfFile: path, encoding: NSUTF8StringEncoding) asString
var ar = read2.componentsSeparatedByString("\n")
let c=ar.count
ar.removeAtIndex(c-1)
var qu = [String](count: ar.count, repeatedValue: " ")
for i in0..<ar.count {
qu[i]=ar[i]
}
q=qu
}
catchlet error asNSError {
// print("ERROR :
reading from file \(fileName) : \(error.localizedDescription)")
}
return q
}
func rite(content: String, _ fileName: String) {
let contentToAppend = content+"\n"
let filePath = NSHomeDirectory() + "/Documents/" + fileName
//Check if file exists
iflet fileHandle = NSFileHandle(forWritingAtPath: filePath) {
//Append to file
fileHandle.seekToEndOfFile()
fileHandle.writeData(contentToAppend.dataUsingEncoding(NSUTF8StringEncoding)!)
}
else {
//Create new file
do {
try contentToAppend.writeToFile(filePath, atomically: true, encoding: NSUTF8StringEncoding)
} catch {
print("Error
creating \(filePath)")
}
}
}
func rite2(content: String,_ fileName: String) {
let contentToAppend = content
let filePath = NSHomeDirectory() + "/Documents/" + fileName
//Check if file exists
iflet fileHandle = NSFileHandle(forWritingAtPath: filePath) {
//Append to file
// fileHandle.seekToEndOfFile()
//
fileHandle.writeData(contentToAppend.dataUsingEncoding(NSUTF8StringEncoding)!)
do {
try content.writeToFile(filePath, atomically: false, encoding: NSUTF8StringEncoding)
} catch {
print("Error
creating \(filePath)")
}
}
else {
//Create new file
do {
try contentToAppend.writeToFile(filePath, atomically: true, encoding: NSUTF8StringEncoding)
} catch {
print("Error
creating \(filePath)")
}
}
}
Look at the line ar.removeAtIndex(c-1) in func doc2 aim to remove last blank line create by "\n".
func rite() write data line next to line.
func rite2 overwrite data in to old data .
We already have declare of tableView, arrays fru, fru2, just add to viewDidLoad.
var t=doc()
var g=""
for i in0..<fru.count {
g=g+fru[i]+"\n"
}
let g2=g.characters.dropLast()
let gi=String(g2)
if(t==0){
rite(gi,"ghi.txt")
}
fru2 = doc2()
First we read file ghi.txt, if t=0 mean there 's no file, we write array data to ghi.txt.
After that we read to new aray fru2 = doc2().
Change array in tableView to fru2.
Copy in to delete function.
var s=""
for i in0..<self.fru2.count{
s = s+self.fru2[i]+"\n"
}
self.rite2("","ghi.txt")
let s2=s.characters.dropLast()
let gi=String(s2)
self.rite(gi, "ghi.txt")
We delete and write again to file ghi.txt.
Now run to see deleted row disappear when reopen.
If want to edit row we do same delete, replace old row with new row and rewrite file ghi.txt.
This is example for array name ani.
ani.removeAtIndex(dong)
ani.insert(info,atIndex:dong)
tableView.reloadData()
var s=""
for i in0..<self.ani.count{
s = s+self.ani[i]+"\n"
}
self.rite2("","ghi.txt")
self.rite(s, "ghi.txt")
No comments:
Post a Comment