获取SectionList项目的索引React-Native

时间:2018-09-29 13:36:40

标签: javascript react-native react-native-sectionlist

我在从SectionList React-Native获取标头项目的索引时遇到问题。通过按标题,我试图获取项目的索引,然后将其传递给函数。我尝试了很多事情,但是没有运气。有什么建议么。谢谢

enter image description here

我想按3-30pm返回索引0 我可以按Lucian返回0 这个想法是通过让我获得标头索引来实现的,我可以使用数组从列表中删除一个项目。

            <SectionList style = {styles.itemSquare}

        renderItem = {({item, index, section}) =>
            < Text style={styles.SectionListItemStyle} key = {index}
            onPress={this.GetSectionListItem.bind(this, this.state.slotkeys[index])}> {item}
            < /Text>}

            renderSectionHeader = {({section: {title}, index}) => (
                  <TouchableHighlight >
                    <View>
                      <Text style={styles.SectionHeaderStyle}
                      onPress={this.GetSectionListItem.bind(this, index)}
                      > {title}
                      <Text style={styles.SectionHeaderCancel} > {index} < /Text>
                      </Text>
                    </View>
                  </TouchableHighlight>
                )
            }

          sections = {this.state.slots.map(({ time, chosen_user, name, chosen_syllabud, user_id }) =>
          ({ title: time, data: [[chosen_user], [chosen_syllabud], [user_id]], index:1 }))}

          keyExtractor = {(item, index) => item + index}
          />

1 个答案:

答案 0 :(得分:1)

renderSectionHeader道具在调用时不会接收索引作为参数,但是您可以修复部分道具以通过地图正确传递索引:

  sections = {this.state.slots.map(({ time, chosen_user, name, chosen_syllabud, user_id }, index) =>
      ({ title: time, data: [[chosen_user], [chosen_syllabud], [user_id]], index }))}

,然后在您的renderSectionHeader上,您可以访问此部分内的索引:

renderSectionHeader = {({section: {title, index}}) => (
              <TouchableHighlight >
                <View>
                  <Text style={styles.SectionHeaderStyle}
                  onPress={this.GetSectionListItem.bind(this, index)}
                  > {title}
                  <Text style={styles.SectionHeaderCancel} > {index} < /Text>
                  </Text>
                </View>
              </TouchableHighlight>
            )
        }