表视图 - 索引超出cellForRowAt的范围

时间:2018-02-02 21:46:43

标签: ios swift uitableview

我在tableView.reloadData()变量的didSet{}内拨打items。 我只在cellForRowAt函数中发生了一次崩溃,从未发生过,从未发生任何代码更改。

override func tableView(_ tableView: UITableView, number ofRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return items.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.tag = indexPath.row
    let item = items[indexPath.row] //Thread 1: Fatal error: Index out of range
    cell.textLabel?.text = item.title

5 个答案:

答案 0 :(得分:2)

您需要处理解决崩溃的可能解决方案。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if(indexPath.row > items.count-1){
        return UITableViewCell()
      }  
    else{

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.tag = indexPath.row
    let item = items[indexPath.row] //Thread 1: Fatal error: Index out of range
    cell.textLabel?.text = item.title

    return cell
  }
}

也许这会有所帮助。

答案 1 :(得分:1)

如果表格快速滚动并且您更新了数据源,我可以看到它发生:它可能会遇到竞争条件,它会在它意识到您更改数据源之前再次尝试调用cellForRowAt。作为预防措施,我建议总是添加一个检查,指出你的索引小于数组的数量。也许这是偏执狂,但比崩溃更好。

e.g:

  1. 数组有100个项目。
  2. 用力轻扫,将你送到桌子底部。
  3. 滚动时,数组会更新为只有10个项目。
  4. 表格滚动要求第99行的单元格,因为它还没有收到消息。
  5. 在只有10个项目的数组中请求项目99时崩溃。

答案 2 :(得分:0)

当您的索引超过数组计数时,会出现此崩溃。

滚动表格视图和表格视图时,可能会出现此崩溃。您正在尝试获取索引而不是数组计数。

您可以检查indexpath.row的条件是否小于数组计数。

答案 3 :(得分:0)

尝试使用像这样的东西

  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.lms</groupId>
    <artifactId>LeadM</artifactId>
    <packaging>war</packaging>
    <version>1.0.1-SNAPSHOT</version>
    <name>LeadM Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <java-version>1.8</java-version>
        <org.springframework-version>4.2.0.RELEASE</org.springframework-version>
        <org.aspectj-version>1.7.4</org.aspectj-version>
        <org.slf4j-version>1.7.5</org.slf4j-version>
        <hibernate.version>5.1.0.Final</hibernate.version>
        <log4j.version>1.2.17</log4j.version>
        <json.version>20140107</json.version>
        <jackson.version>1.9.10</jackson.version>
        <org.springframework.social-version>1.1.2.RELEASE</org.springframework.social-version>
        <org.springframework.social.facebook-version>1.0.0.RELEASE</org.springframework.social.facebook-version>
        <org.springframework.social.twitter-version>1.0.0.RELEASE</org.springframework.social.twitter-version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spring.security.version>3.2.3.RELEASE</spring.security.version>

    </properties>

    <dependencies>

        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>



        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-envers</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-c3p0</artifactId>
            <version>${hibernate.version}</version>
        </dependency>


        <!-- Apache Commons DBCP -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <!-- Spring ORM -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>




        <!-- Logging -->
        <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> 
            <version>${org.slf4j-version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> 
            <artifactId>jcl-over-slf4j</artifactId> <version>${org.slf4j-version}</version> 
            <scope>runtime</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> 
            <artifactId>slf4j-log4j12</artifactId> <version>${org.slf4j-version}</version> 
            <scope>runtime</scope> </dependency> <dependency> <groupId>log4j</groupId> 
            <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency>
        <!-- @Inject -->
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

        <!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>

        <!-- Jackson JSON Mapper -->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>${json.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.7.3</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.7.3</version>
        </dependency>


        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.7.3</version>
        </dependency>

        <!-- MySql 5.5 Connector -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.29</version>
        </dependency>
        <!-- Apache POI -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.13</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.4</version>
        </dependency>


        <!-- commons-io -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3</version>
        </dependency>

        <!--opencsv -->

        <dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>3.6</version>
        </dependency>
        <dependency>
            <groupId>commons-validator</groupId>
            <artifactId>commons-validator</artifactId>
            <version>1.5.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.6.2</version>
        </dependency>


        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.0.1</version>
        </dependency>
        <!--java mail -->

        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.5.5</version>
        </dependency>


        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>dsn</artifactId>
            <version>1.5.5</version>
        </dependency>

        <dependency>
            <groupId>com.maxmind.geoip</groupId>
            <artifactId>geoip-api</artifactId>
            <version>1.2.14</version>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.1.5</version>
        </dependency>

        <dependency>
            <groupId>com.cronutils</groupId>
            <artifactId>cron-utils-spring</artifactId>
            <version>1.0.1</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/com.sendgrid/sendgrid-java -->
        <!--To send mails from campaigning module via send grid api -->
        <dependency>
            <groupId>com.sendgrid</groupId>
            <artifactId>sendgrid-java</artifactId>
            <version>3.1.0</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/com.sendgrid/smtpapi-java -->
        <dependency>
            <groupId>com.sendgrid</groupId>
            <artifactId>smtpapi-java</artifactId>
            <version>1.2.0</version>
        </dependency>

        <dependency>
            <groupId>com.restfb</groupId>
            <artifactId>restfb</artifactId>
            <version>1.34.1</version>
        </dependency>

        <dependency>
            <groupId>com.google.api-client</groupId>
            <artifactId>google-api-client</artifactId>
            <version>1.22.0</version>
        </dependency>

        <dependency>
            <groupId>com.google.apis</groupId>
            <artifactId>google-api-services-analyticsreporting</artifactId>
            <version>v4-rev114-1.22.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.api-client</groupId>
            <artifactId>google-api-client-gson</artifactId>
            <version>1.22.0</version>
        </dependency>

        <!-- gcm push notification -->
        <!-- https://mvnrepository.com/artifact/com.ganyo/gcm-server -->
        <dependency>
            <groupId>com.ganyo</groupId>
            <artifactId>gcm-server</artifactId>
            <version>1.0.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>
        <!-- gcm push notification -->
        <!-- https://mvnrepository.com/artifact/mstor/mstor -->
        <dependency>
            <groupId>mstor</groupId>
            <artifactId>mstor</artifactId>
            <version>0.9.7</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/jdom/jdom -->
        <dependency>
            <groupId>jdom</groupId>
            <artifactId>jdom</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>net.sf.biweekly</groupId>
            <artifactId>biweekly</artifactId>
            <version>0.6.1</version>
        </dependency>



        <dependency>
            <groupId>com.firebase</groupId>
            <artifactId>firebase-client</artifactId>
            <version>1.0.11</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.google.oauth-client/google-oauth-client -->
        <dependency>
            <groupId>com.google.oauth-client</groupId>
            <artifactId>google-oauth-client</artifactId>
            <version>1.22.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.google.oauth-client/google-oauth-client-jetty -->
        <dependency>
            <groupId>com.google.oauth-client</groupId>
            <artifactId>google-oauth-client-jetty</artifactId>
            <version>1.22.0</version>
        </dependency>



        <!-- https://mvnrepository.com/artifact/com.google.apis/google-api-services-gmail -->
        <dependency>

            <groupId>com.google.apis</groupId>
            <artifactId>google-api-services-gmail</artifactId>
            <version>v1-rev70-1.18.0-rc</version>

        </dependency>


        <dependency>
            <groupId>org.apache.servicemix.bundles</groupId>
            <artifactId>org.apache.servicemix.bundles.geocoder-java</artifactId>
            <version>0.16_1</version>
        </dependency>



        <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.9</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/com.google.http-client/google-http-client -->
        <dependency>
            <groupId>com.google.http-client</groupId>
            <artifactId>google-http-client</artifactId>
            <version>1.16.0-rc</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.google.http-client/google-http-client-jackson2 -->
        <dependency>
            <groupId>com.google.http-client</groupId>
            <artifactId>google-http-client-jackson2</artifactId>
            <version>1.22.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.google.code.geocoder-java/geocoder-java -->
        <dependency>
            <groupId>com.google.code.geocoder-java</groupId>
            <artifactId>geocoder-java</artifactId>
            <version>0.16</version>
        </dependency>

    <!--azure blob storage -->
    <dependency>
        <groupId>com.microsoft.azure</groupId>
        <artifactId>azure-storage</artifactId>
        <version>6.1.0</version>
    </dependency>
<!-- azure redis cache - Java client for Redis  https://github.com/xetorthio/jedis -->
 <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.5.0.RELEASE</version>
</dependency> 

    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>

    <!-- Spring Security -->
    <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${spring.security.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${spring.security.version}</version>
        </dependency>

        <!-- Spring Security JSP Taglib -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>${spring.security.version}</version>
        </dependency>


</dependencies> 
    <build>
        <finalName>ROOT</finalName>

        <!-- <plugins> <plugin> <groupId>org.eclipse.m2e</groupId> <artifactId>lifecycle-mapping</artifactId> 
            <version>1.0.0</version> <configuration> <lifecycleMappingMetadata> <pluginExecutions> 
            <pluginExecution> <pluginExecutionFilter> <groupId>net.alchim31.maven</groupId> 
            <artifactId>yuicompressor-maven-plugin</artifactId> <version>1.3.0</version> 
            <executions> <execution> <id>compressyui</id> <phase>process-resources</phase> 
            <goals> <goal>jslint</goal> <goal>compress</goal> </goals> <configuration> 
            <nosuffix>true</nosuffix> <warSourceDirectory>${basedir}/WebContent</warSourceDirectory> 
            <webappDirectory>${basedir}/src/main/webapp/resources/js/appJs</webappDirectory> 
            <jswarn>false</jswarn> </configuration> </execution> </executions> </pluginExecutionFilter> 
            </pluginExecution> </pluginExecutions> </lifecycleMappingMetadata> </configuration> 
            </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <executions> 
            <execution> <id>default-war</id> <phase>package</phase> <goals> <goal>war</goal> 
            </goals> <configuration> <warSourceDirectory>${basedir}/WebContent</warSourceDirectory> 
            <encoding>UTF-8</encoding> </configuration> </execution> </executions> <configuration> 
            <warSourceDirectory>${basedir}/WebContent</warSourceDirectory> <encoding>UTF-8</encoding> 
            <webResources> <resource> <directory>${basedir}/src/main/webapp/resources/js/appJs</directory> 
            </resource> </webResources> </configuration> </plugin> </plugins> -->


    </build>


</project>

是否需要这样做?

崩溃索引超出范围

您是否在此didSet声明后重新更新数组?如果是,请不要在此使用此DidSet案例,如果不再更新Array,请使用

如果你还想使用,也可以在didSet()

之后使用WillSet Case

答案 4 :(得分:-1)

override func tableView(_ tableView: UITableView, number ofRowsInSection section: Int) -> Int {
    guard let items.count > 0 else {return 0}
    return items.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.tag = indexPath.row
    let item = items[indexPath.row] 
    cell.textLabel?.text = item.title
}

我认为items数组没有任何值,所以它会崩溃。所以,试试上面的代码。