2012-05-08 131 views
0

我有我在我的每个对象的属性的一个UItable正在填充对象的数组是一个YES/NO的值使用 -xcode中的UITableView

IM(的UITableViewCell *)的tableView:(UITableView的*)的tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {填充我的表如下。

我的代码为数组中的每个对象创建一个表条目。我想只使用数组中具有“显示”属性设置为YES的对象来填充表格?我该怎么做呢?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
static NSString *cellidentifier = @"customcell"; 
customcell *cell = (customcell *)[tableView dequeueReusableCellWithIdentifier: 
            cellidentifier]; 

my_details *myObj = [appDelegate.myArray objectAtIndex:indexPath.row]; 

// UITableViewCell cell needs creating for this UITableView row. 
if (cell == nil) 

{ 
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"customcell" owner:self options:nil]; 

    for (id currentObject in topLevelObjects) { 
     if ([currentObject isKindOfClass:[customcell class]]) { 
      cell = (customcell *) currentObject; 
      break; 
     } 
    } 
} 


    cell.line1.text = myObj.line1; 
    cell.line2.text = myObj.line2; 
    cell.line3.text = myObj.line3; 


return cell; 
} 

回答

0

这是一个好主意,试图过滤对象,然后将它们发送到表中显示。

您可以使用NSPredicate过滤对象数组,并在对象更改状态时将display属性设置为yes,重新过滤数组并将其传递给tableview。

NSPredicate *testForTrue = [NSPredicate predicateWithFormat:@"display == YES"]; 
NSArray *filteredArray = [myObjectsArray filteredArrayUsingPredicate:testForTrue]; 

获得filteredArray后,您需要将它传递给tableView并告诉它重新加载它的数据以便刷新。

0

As @Sorin表示您必须过滤您的表格。这可以在插入表格之前或插入过程中完成。哪一个是品味的问题(和准则,你通常遵循编程)

一)前 拥有所有数据 - >过滤器 - >已减少集 - >显示减小的表

b)在 有所有的数据 - >计数#items - >引入行 - >显示表 对于您必须适应numberOfRowsInSection

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    counter=0; 
    for each in table 
    if value = YES counter++ 
    return counter; 
} 

在你现在必须跟踪下一行到的cellForRowAtIndexPath(伪代码!)通过全局变量插入:(伪代码!)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell; 
    static NSString *CellIdentifier = @"Cell"; 
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    rowcounter++; 
    cell.textLabel.text=[selectionTableArray objectAtIndex:rowcounter]; 
    return cell; 
}