1

I use 2 foreach loops in my widget body, which slow down the performance (Code completion is slow). Also there are many HStacks and VStacks. Is there a way around this?

  ForEach(0..<data.eventsToday.count)
            {(row) in
                
                if row < 3 {
                    HStack{
                        Rectangle().fill(eventBgColor)
                            .frame(width: 6, height: 25, alignment: .leading)
                            .cornerRadius(3.0)
                        VStack{
                            Text(data.eventsToday[row].title)
                                .font(eventFont)
                                .multilineTextAlignment(.leading)
                                .frame(width: 120.0, height: 14.0, alignment: .bottomLeading)
                                .truncationMode(/*@START_MENU_TOKEN@*/.tail/*@END_MENU_TOKEN@*/).lineLimit(1)
                                .foregroundColor(titleColor)
                            
                            Text("\(String(MediumWidget2GetStartTime(indexRow:row)))")
                                .font(timeFont)
                                .multilineTextAlignment(.leading)
                                .frame(width: 120.0, height: 11.0, alignment: .bottomLeading)
                                .lineLimit(1)
                                .foregroundColor(timeColor)
                        }
                    }.padding(.bottom, 5.0)
                }
            }
     
0

1 Answer 1

1

If you want to show just 3 records, then it looks more appropriate to limit data container (and don't iterate all events), like

ForEach(0..<data.eventsToday.prefix(3).count)
    {(row) in
        HStack{
            Rectangle().fill(eventBgColor)
                .frame(width: 6, height: 25, alignment: .leading)
                .cornerRadius(3.0)
            VStack{
                Text(data.eventsToday[row].title)
                    .font(eventFont)
                    .multilineTextAlignment(.leading)
                    .frame(width: 120.0, height: 14.0, alignment: .bottomLeading)
                    .truncationMode(/*@START_MENU_TOKEN@*/.tail/*@END_MENU_TOKEN@*/).lineLimit(1)
                    .foregroundColor(titleColor)
                
                Text("\(String(MediumWidget2GetStartTime(indexRow:row)))")
                    .font(timeFont)
                    .multilineTextAlignment(.leading)
                    .frame(width: 120.0, height: 11.0, alignment: .bottomLeading)
                    .lineLimit(1)
                    .foregroundColor(timeColor)
            }
        }.padding(.bottom, 5.0)
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you , and for the 2. Foreach i want to start at index 3 to index 6, how would i do that? Like so..? ForEach(3..<data.eventsToday.prefix(3).count). Would It crash if there are less than 4 entries?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.