小编fs_*_*gre的帖子

从QTableView读取和写入文件

如何读取和写入文本文件日期进入QTableView?

这就是我所拥有的,但我希望在将数据添加到表中时保存数据,当然可以在重新打开应用程序时将其读回.有没有我可以参考的教程?

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    model = new QStandardItemModel();
    model->setRowCount(0);
    ui->tableView->setModel(model);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    QStandardItem *userName = new QStandardItem(ui->lineEdit_Name->text());
    QStandardItem *userNumber = new QStandardItem(ui->lineEdit_Number->text());

    QList<QStandardItem*> row;
    row <<userName << userNumber;

    model->appendRow(row);
}
Run Code Online (Sandbox Code Playgroud)

非常感谢

编辑--------------------------------

这对我有用:

添加功能:

 void MainWindow::on_pushButto_Add_clicked() {
     QStandardItem *userInput = new QStandardItem(ui->lineEdit->text());
     QStandardItem *userInput2= new QStandardItem(ui->lineEdit_2->text());

     QList<QStandardItem*> row;
     row <<userInput << userInput2;

     model->appendRow(row); 
}
Run Code Online (Sandbox Code Playgroud)

保存功能:

void MainWindow::on_pushButton_Save_clicked()
{
    QFile file("C:/Users/UserName/Practicing/Resources_Files/someFile.bin");

     if (file.open(QIODevice::WriteOnly))
     {
         QDataStream stream(&file);
         qint32 n …
Run Code Online (Sandbox Code Playgroud)

qt qtableview qstandarditemmodel qmodelindex

2
推荐指数
1
解决办法
8068
查看次数

将项目附加到保存在plist中的NSDictionary中的现有密钥

如何从plist中将值附加到NSDictionary中的现有键?

我所拥有的基本上是一个保存在光盘中的带有几个键和值的plist,键是一些具有一个初始值的学生的名字.我试图做的不起作用是通过读取plist将更多项添加到现有键/学生,将其添加到临时NSDictionary,将临时数组附加到plist中的现有键但是当我保存时plist back它不起作用,它只保存最后两项并基本上删除该键的初始值.

这是我正在使用的代码......

- (IBAction)testing:(id)sender
{
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [dirPaths objectAtIndex:0];
    NSString *fullPath = [documentsPath stringByAppendingPathComponent:@"studentsRecords.plist"];

    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:fullPath];

    if(fileExists)
    {
        NSMutableDictionary *dictionaryRecords = [[NSMutableDictionary alloc]initWithContentsOfFile:fullPath];
        NSLog(@"Items for Nathan: %@",[dictionaryRecords objectForKey:@"Nathan"]);// here the output is, Items for Nathan: Ruler
        // which make sense since I only had one initial record for Nathan in my plist,

        // Here, I want to add two more items to the key Nathan by appending …
Run Code Online (Sandbox Code Playgroud)

objective-c nsdictionary plist ios

2
推荐指数
1
解决办法
2430
查看次数

问题使用Swift中的prepareForSegue方法传递自定义单元格的内容

我有一个UITableView包含标签的自定义单元格.我想要的是能够在选择行时将此标签的内容传递给第二个视图控制器.
我在没有自定义单元格的表格上运行的代码很好:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("reusableCell", forIndexPath: indexPath) as! CustomCell

    //labelDisplayLedQty is the name of the custom label
    cell.labelDisplayLedQty.text = inputLedQty.text

    return cell
}
Run Code Online (Sandbox Code Playgroud)

这是我在该prepareForSegue方法中的代码.正如我所说,如果我将它与不包含自定义单元格的表一起使用,此代码可以正常工作.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    let rowIndex: NSIndexPath = tableList.indexPathForSelectedRow!
    let selectedRow: UITableViewCell = tableList.cellForRowAtIndexPath(rowIndex)!
    let contentFromSelectedRow: String = selectedRow.textLabel!.text!

    if let secondVC = segue.destinationViewController as? DriverDetailsViewController where
       segue.identifier == "segueDriverDetails" {
        secondVC.messageContentFromMainController = contentFromSelectedRow + " This message is …
Run Code Online (Sandbox Code Playgroud)

uitableview ios swift

2
推荐指数
1
解决办法
539
查看次数

将在整个应用程序或类中使用的颜色

我正在努力提高我的面向对象技能,而且我总是在争论是否需要课程.

我有一组UIColors,我一直在不同的ViewControllers中使用我的应用程序,我最初开始时将它们添加为常量全局,如下所示...

import UIKit
// Global COLORS
let myBlueColor = UIColor(red: 62.0/255, green: 174.0/255, blue: 206.0/255, alpha: 1.0)
// more global colors here...
class ViewController1{
   // using my global color
   myButton.layer.borderColor = myBlueColor.CGColor
}


// other viewController
import UIKit
class ViewController2{
   // using my global color again
   myButton2.layer.borderColor = myBlueColor.CGColor
}
Run Code Online (Sandbox Code Playgroud)

但后来我决定创建一个类来强迫自己以更加面向的方式思考......

颜色类

import Foundation
import UIKit

class Color {

    var myBlueColor:UIColor{
        get{
          return UIColor(red: 62.0/255, green: 174.0/255, blue: 206.0/255, alpha: 1.0)
        }
    }
    var myLightGrayColor:UIColor{
        get{
            return UIColor(red: 249.0/255, …
Run Code Online (Sandbox Code Playgroud)

oop ios swift

2
推荐指数
1
解决办法
537
查看次数

在Swift中重用UIButton属性的最佳方法是什么?

我有一堆按钮,UIToolbar当我UITextField点击使用时,我正在添加这些按钮inputAccessoryView.所有这些按钮都是相同的标题以外的什么,我想能够做的就是重用UI属性,如titleColor,frame等.

什么是完成我上面描述的最有效的方法?

这是代码......

    // code for first button
    let button1 = UIButton();
    button1.backgroundColor = UIColor.orangeColor()
    button1.setTitle("button1", forState: .Normal)
    button1.setTitleColor(UIColor.whiteColor(), forState: .Normal)
    button1.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
    button1.frame = CGRect(x:0, y:0, width:35, height:35)
    button1.addTarget(self, action: #selector(myFunction), forControlEvents: UIControlEvents.TouchUpInside)

    // code for second button which is identical to the first button
    let button2 = UIButton();
    button2.backgroundColor = UIColor.orangeColor()
    button2.setTitle("button2", forState: .Normal)
    button2.setTitleColor(UIColor.whiteColor(), forState: .Normal)
    button2.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
    button2.frame = CGRect(x:0, y:0, width:35, height:35)
    button2.addTarget(self, …
Run Code Online (Sandbox Code Playgroud)

uibutton uitextfield uitoolbar ios swift

2
推荐指数
1
解决办法
187
查看次数

在UITableView中取消选择行时如何从数组中删除项目

我正在尝试从中捕获所选项目UITableView并将其保存到新数组中。下面的代码通过在点击行时添加项目来创建新数组,而当取消选择行时,它不执行的操作是删除项目。

newFruitListUITableView取消选择a中的行时,如何从中删除项目?

还是更好的方法是,仅生成一个选定项目的数组的正确方法是UITableView什么?

 class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate  {

    let fruits = ["Apples", "Oranges", "Grapes", "Watermelon", "Peaches"]

    var newFruitList:[String] = []

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return fruits.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = UITableViewCell()
        cell.textLabel?.text = fruits[indexPath.row]
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { …
Run Code Online (Sandbox Code Playgroud)

uitableview swift

2
推荐指数
1
解决办法
944
查看次数

如果项目存在于 C#/WPF 中的 ObservableCollection 中,则查找数字的索引

在下面的代码中,我正在检查某个项目是否存在于 an 中ObservableCollection,如果存在,我想获取它的索引,以便我可以将该项目移动到顶部,但我无法弄清楚。

我的对象:

public class RecentFile
{
    public string FileName { get; set; }
    public string DateAdded { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

用于查找项目索引的代码:

if (recentFilesObservableCollection.Any(f => f.FileName == "MyFileName")) {

    foreach (RecentFile file in recentFilesObservableCollection) {
        if (file.FileName == "MyFileName") {
            var x = RecentFilesDataGrid[(recentFilesObservableCollection.IndexOf(file)];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果项目存在,获取索引号的最佳方法是什么?

最终我需要做的是......

  1. 检查项目是否存在
  2. 如果确实将其移至列表顶部

c#

2
推荐指数
1
解决办法
2536
查看次数

如何在Vim中快速向上和向下重新映射(ctrl u和ctrl d)到ctrl j和ctrl k

如何重新映射快速上下命令?
我想重新映射Ctrl-UCtrl-KCtrl-DCtrl-J

我试过以下但没有运气......

nnoremap <c-u> <c-k> 
nnoremap <c-d> <c-j>
Run Code Online (Sandbox Code Playgroud)

仅供参考 - Vim安装在Ubuntu Server中

vim

1
推荐指数
1
解决办法
362
查看次数

转到没有segues的第二个viewController

在下面的代码中我有一个动作/按钮,当点击时带你到另一个viewController(SecondViewController),一切正常,但有一行我不完全理解的代码.

这行代码在做什么?

secondVC.delegate = self

我们在这里谈论的代表是什么?如果我只需要转到其他视图并且我没有传递任何数据,那真的需要吗?

@IBAction func goToView(sender: AnyObject) {  
    let secondVC= storyboard?.instantiateViewControllerWithIdentifier("secondViewController") as! SecondViewController  
    secondVC.delegate = self // is this needed?  
    self.presentViewController(secondVC, animated:true, completion:nil)  
} 
Run Code Online (Sandbox Code Playgroud)

delegates ios segue swift

1
推荐指数
1
解决办法
1616
查看次数

了解Swift中货币的NumberFormatter

我有一个应用程序,我正在尝试格式化Double,NumberFormetter但我得到的输出与其他应用程序中的输出不匹配.

这是我正在使用的代码......

let price:Double = 25
let currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = true
currencyFormatter.numberStyle = .currency
currencyFormatter.locale = NSLocale.current

let priceFormatter = currencyFormatter.string(for: price)

myLabel.text = priceFormatter
Run Code Online (Sandbox Code Playgroud)

使用上面的代码,并且Region在我的应用程序中将手机设置为中国,我得到的25,00 CNY是另外两个第三方应用程序的输出CN¥25.00.

我究竟做错了什么?

地区设为中国:

我的应用程序
25,00 CNY

其他应用程序
¥25.00

地区设置为法国:

我的应用程序
25,00€

其他应用程序
€25,00

但是当该区域设置为United States我在所有三(3)个应用程序中得到相同的表示法.

地区设置为美国:

我的应用程序
$ 25.00

其他应用
$ 25.00

我有点担心尤其是当区域设置为中国的输出,因为除了符号是不同的我也得到了不同的符号CNYVS CN¥.

有什么建议,我做错了吗?

number-formatting ios swift

1
推荐指数
1
解决办法
625
查看次数