Notice
Recent Posts
Recent Comments
Link
ยซ   2024/05   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

lgvv98

[SwiftUI] List Row ์„ ํƒํ•˜๊ธฐ(TableView didSelectRow) ๋ณธ๋ฌธ

iOSํ”„๋กœ์ ํŠธ/โ˜˜๏ธ Kuring

[SwiftUI] List Row ์„ ํƒํ•˜๊ธฐ(TableView didSelectRow)

๐Ÿฅ• ์บ๋Ÿฟ๋งจ 2022. 5. 30. 20:50

List Row ์„ ํƒํ•˜๊ธฐ(TableView didSelectRow)

 

โœ… ์˜ค๋žœ๋งŒ์ด๋‹ค! ์ œ๋ชฉ์ด ์ฐธ UIKit์Šค๋Ÿฝ๋‹ค.

 

์ œ๋ชฉ์„ ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•˜๊ฒŒ ๋œ ์ด์œ ๋กœ๋Š” ๋‚ด๊ฐ€ 1๋…„ ์ •๋„ UIKit์„ ์‚ฌ์šฉํ•˜๋ฉด์„œ ์ด๋ฏธ ์‚ฌ๊ณ ๊ฐ€ ์—ฌ๊ธฐ์— ์ต์ˆ™ํ•ด์ง„ ์ƒํƒœ์ธ๋ฐ, SwiftUI๋กœ List์˜ Row(UIKit์—์„œ cell)์„ ์„ ํƒํ•˜๋Š” ๋กœ์ง์„ ์ž‘์„ฑํ•˜๋Š”๊ฒŒ ์ž˜ ์•ˆ๋˜์—ˆ๋‹ค.๊ทธ๋ž˜์„œ ๋‚ด๊ฐ€ ๊ตฌ๊ธ€์— ์ € ํ‚ค์›Œ๋“œ๋กœ ๊ฒ€์ƒ‰ํ–ˆ๋Š”๋ฐ, ๋”ฑํžˆ ๋ง˜์— ๋“œ๋Š”๊ฒŒ ์•ˆ๋‚˜ํƒ€๋‚˜์„œ ์ง์ ‘ ์ •๋ฆฌํ•˜๋ ค๊ณ  ํฌ์ŠคํŒ… ํ•จ.

 

์ƒ๊ฐ๋ณด๋‹ค ๊ฐ„๋‹จํ•œ๋ฐ, ์ด๊ฒŒ ์ฐธ ์–ด๋ ค์› ์Œ.

์šฐ์„  UIKit์„ ์‚ฌ์šฉํ•˜๋‹ค๋ณด๋‹ˆ tableView์—์„œ๋Š” UITableViewDelegate๋ฅผ ์ƒ์†๋ฐ›์•„์„œ didSelectRow๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด ๋˜์—ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ์ด์™€ ๋น„์Šทํ•˜๊ฒŒ List์˜ Row์— onTapGesture๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ ๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐ!!

 

โœ… ์ž˜๋ชป๋œ case

struct didSelectRow: View {
    var body: some View {
        List {
            ForEach((0...10), id: \.self) { index in
                Text("\(index)")
                    .onTapGesture {
                        print("\(index)")
                    }
            }
        }
    }
}

struct didSelectRow: View {
    var body: some View {
        List {
            ForEach((0...10), id: \.self) { index in
                Text("\(index)")
            }.onTapGesture {
                print("didTap")
            }
        }
    }
}

์œ„์˜ ๋‘๊ฐ€์ง€ ๊ฒฝ์šฐ์—๋Š” Text ์˜์—ญ์—๋งŒ touch๊ฐ€ ๋˜์–ด์„œ row์ „์ฒด๋ฅผ ์ปค๋ฒ„ํ•  ์ˆ˜ ์—†์Œ.

 

โœ… ๊ทธ๋ ‡๋‹ค๋ฉด List ์ž์ฒด์— onTapGesture๋ฅผ ๋ถ™์ด๊ณ  ํ•ด๋‹นํ•˜๋Š” ์ •๋ณด๋ฅผ ์–ป์–ด๋ณผ๊นŒ?

struct didSelectRow: View {
    var body: some View {
        List {
            ForEach((0...10), id: \.self) { index in
                Text("\(index)")
            }
        }.onTapGesture {
            print("didTap")
        }
    }
}

์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•˜๋ฉด List์ „์ฒด๊ฐ€ ์„ ํƒ๋œ๋‹ค. ํ•˜์ง€๋งŒ row์•ˆ์— ํ•ด๋‹นํ•˜๋Š” ์ •๋ณด๋ฅผ ์–ป์„ ์ˆ˜ ์—†์Œ.

 

๊ทธ๋ ‡๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ?

 

โœ… ์˜ฌ๋ฐ”๋ฅธ case

import SwiftUI

struct didSelectRow: View {
    var body: some View {
        NavigationView {
            List {
                ForEach((0...10), id: \.self) { index in
                    NavigationLink {
                        secondView(index: index)
//                        nextView(index: index)
                    } label: {
                        Text("\(index)")
                    }
                    
                    //                Text("\(index)")
                }
            }
        }
    }
    
    func secondView(index: Int) -> some View {
        var body: some View {
            Text("\(index)")
        }
        
        return body
    }
}

struct nextView: View {
    let index: Int!
    var body: some View {
        Text("\(index)")
    }
}

 

NavigationView๋กœ ๋ฌถ์–ด์ค€ ๋‹ค์Œ์— NavigationLink๋ฅผ ํ†ตํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค!

 (์ค‘์š”) ๋„ค๋น„๊ฒŒ์ด์…˜ ๋ทฐ๋กœ ๋ฌถ๋Š”๋‹ค. -> ์—†์œผ๋ฉด NavigationLink ์ž‘๋™์•ˆํ•จ

 

์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” ์ถ”๊ฐ€๋กœ ๋ทฐ๋ฅผ func์œผ๋กœ๋„ ๊ตฌ์„ฑํ•ด ๋ณด์•˜๋‹ค!

 

โœ… NavigationLink์— ๋Œ€ํ•ด์„œ ์ข€ ๋” ์•Œ์•„๋ณด์ž

NavigationLink { 
	// action
} label: { 
	// ๋ชจ์–‘!
}

๋ฒ„ํŠผ์ด๋ž‘ ๋˜‘๊ฐ™๋‹ค. label์ด ๊ฒ‰๋ชจ์–‘์ด ๋˜๋ฏ€๋กœ ์ด ๋ชจ์–‘์„ ํด๋ฆญํ•˜๋ฉด action์„ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค.

๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ๋ชจ์–‘์ด row๊ฐ€ ๋˜์–ด์•ผ ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค!!

๊ฒฐ๊ณผ๋ฌผ UI

 

 

์ด์ œ ํ•ด๊ฒฐํ•ด์•ผํ•  ๋ฌธ์ œ๊ฐ€ ํ•˜๋‚˜ ๋‚จ์•˜๋‹ค. ๋ฐ”๋กœ ๊ฐ row์˜ accessory ๋ถ€๋ถ„ ์ด๋ฅผ ์—†์• ๋Š” ๋ฐฉ๋ฒ•์„ ๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ ์•Œ์•„๋ณด์ž!

 

2022.05.31 - [iOSํ”„๋กœ์ ํŠธ/Kuring] - [SwiftUI] List accessory (feat. disclosure indicator)

 

[SwiftUI] List accessory (feat. disclosure indicator)

List accessory (feat. disclosure indicator) โœ… ์ด๋ฒˆ์—๋„ ์ œ๋ชฉ์ด ๋‹ค์†Œ UIKit์Šค๋Ÿฝ๋‹ค. ์ €๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋„ ์–ธ๊ธ‰ํ–ˆ์ง€๋งŒ, UIKit์„ ๋จผ์ € ๊ณต๋ถ€ํ•˜๊ณ  SwiftUI๋ฅผ ํ•˜๋‹ค๋ณด๋‹ˆ๊นŒ, ๊ฒ€์ƒ‰๋„ UIKit์Šค๋Ÿฝ๊ฒŒ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—! StackOv..

rldd.tistory.com

 

Comments