按下按钮时添加表格视图单元格

时间:2020-10-03 00:23:10

标签: swift uitableview tableview func

我希望我的快捷代码在按下按钮时添加另一个tableview单元格。现在有两个tableview单元格。该按钮是橙色按钮,按下后应添加第三个绿色的Tabview单元格。添加第三个单元格的功能是addCell。还要在numberOfRowsInSelection中查找定义2个单元格的区域。

enter image description here

import java.util.Scanner;
import java.util.HashMap;
import java.net.*;
import java.io.*;
import java.lang.Thread;

class map{ // functionality for the hashmap

    static int dist = 0;
    static HashMap<String, String> Distances = new HashMap<String, String>(); 

    public static void hMap(int port){
    
    try{ 
        InetAddress ip = InetAddress.getLocalHost();
        Distances.put(Integer.toString(port), Integer.toString(dist));
      
    } catch (UnknownHostException e){
        e.printStackTrace();
    }
    dist += 1;
    }
    public static void printMap(){
      System.out.println(Distances);
    }
    
}            

public class Server{ // main driver class

    public static int xPort;
    public static void main(String[] args){

    if (args.length < 1) return; // safety check
    xPort = Integer.parseInt(args[0]); 

    map x = new map();
    x.hMap(xPort);
    
    menu mThread = new menu(); // new thread for the menu
            
    try (ServerSocket serverSocket = new ServerSocket(xPort)) {
        System.out.println("server is listening on port " + xPort);

        while(true){
        Socket socket = serverSocket.accept();
        System.out.println("Client connected");      
        serverSocket.close();
        System.exit(0);
        }
    } catch(IOException ex) {
        System.out.println("exception");
        ex.printStackTrace();
    }

    
    }

    public static void printPort(){
    System.out.println(xPort);
    }
    
}

class menu implements Runnable{ 
    menu(){
    Thread m = new Thread(this);
    m.run();
    }
    public void run(){ 
    printMenu();
    }
    public void printMenu(){
    
    Scanner input = new Scanner(System.in);
    int choice = 0;
    int newPort = 0;
    System.out.println("1. help\n2. routing\n3. myPort\n4. New connection\n5. exit thread");
    while(choice != 5){
        choice = input.nextInt();
        if (choice == 1)
        System.out.println("4 to exit thread. otherwise make another selection");
        else if (choice == 2){
        map y = new map();
        y.printMap();
        }
        else if (choice == 3){
        Server p = new Server();
        p.printPort();
        }
        else if (choice == 4){ // here is where the issue begins
        System.out.println("port number?");
        newPort = input.nextInt();

        try (ServerSocket serverSocket = new ServerSocket(newPort)) {
            System.out.println("server is listening on port " + newPort);

            while(true){
            Socket socket = serverSocket.accept();
            System.out.println("Client connected");      
            serverSocket.close();
            System.exit(0);
            }
        } catch(IOException ex) {
            System.out.println("exception");
            ex.printStackTrace();
        }
        }
        else if (choice > 5 || choice < 1)
        System.out.println("try again");
    }
    }
}

1 个答案:

答案 0 :(得分:0)

这是a lot之前的要求...

您要返回数据源中的项数(我假设是tableView(_:numberOfRowsInSection:)),而不是在arr中返回硬编码值。

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

然后,只需添加一行即可

func addRowToEnd() {
    arr.append(5) /// not sure what the numbers mean though...
    tableView.beginUpdates()
    tableView.insertRows(at: [IndexPath(row: arr.count - 1, section: 0)], with: .automatic) /// animate the insertion
    tableView.endUpdates() 
}