2014-03-31 36 views
2

我试图让我的头部使用TableView索引属性来创建本地右手字母导航 - 类似于Apple iOS Contacts应用程序中的 - 类似于下图所示:Appcelerator Titanium JS:表按字母顺序排列的索引不起作用

enter image description here

我创建了一个非常简单的例子,有一组行,有行标题,但它不工作 - 每当我对索引项挖掘,它只是跳转到的顶部再次查看TableView。

这里是我的示例代码:

// Create the first TableViewSection 
var section1 = Ti.UI.createTableViewSection({ 
    headerTitle:'A' 
}); 
// use a loop to add some rows 
for (var i=0; i < 20; i++) { 
    section1.add(Ti.UI.createTableViewRow({ 
     title:'Row '+i 
    })); 
} 
// do it all again... 
var section2 = Ti.UI.createTableViewSection({ 
    headerTitle: 'B' 
}); 
for (var i=4; i < 10; i++) { 
    section2.add(Ti.UI.createTableViewRow({ 
     title:'Row '+i 
    })); 
} 
// Now, here's our table, and we're setting the data to hold the sections 
var tv = Ti.UI.createTableView({ 
    data:[section1,section2] 
}); 

// Create a table view index 
var index = [ 
    { title: "A", index: 0 }, 
    { title: "B", index: 1 } 
]; 

// Set the index on the table view 
tv.setIndex(index); 

$.index.open(); 
$.index.add(tv); 

回答

4

这里是你亲爱的

var win = Ti.UI.createWindow(); 

var table = Ti.UI.createTableView({}); 

    var contacts = ["Adam", "Andrew", "Boris", "Claus", "Debby", 'Saba', 'Sana', 'Wahhab', 'Zohaib', 'Zzaid', 'Zzxad']; 
    var curheader = 'A'; 
    var sectionArr = []; 
    var index = []; 
    for (var i = 0, lastL, l, currSection, ilen = contacts.length; i < ilen; i++) { 
     l = contacts[i].substr(0, 1); 
     if (lastL != l) { 
      index.push({ 
       title : l, 
       index : i 
      }); 
      currSection = Ti.UI.createTableViewSection({ 
       headerTitle : l 
      }); 
      sectionArr.push(currSection); 
     } 
     currSection.add(Ti.UI.createTableViewRow({ 
      title : contacts[i], 

     })); 
     lastL = l; 

    } 
    table.setData(sectionArr); 
    table.index = index; 
    win.add(table); 
    win.open(); 

感谢

+0

感谢一个例子!我只是想知道,部分是强制性的,还是可以在没有它们的情况下实现这一点? – shrewdbeans

+0

部分是强制显示行的bc d,并且它是否帮助不要忘记将线程标记为已回答 –