2011-04-27 74 views
1

我正在使用tableview,我想要的行數(即tableview中的項目數)。如何獲得行數?tableview的行數

請幫助我, 在此先感謝。

+0

您設置了numberOfRowsinSection中的行數,因此您已經知道。 – dks1725 2011-04-27 11:39:04

回答

5

在頭文件中帶一個實例變量。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  { 
    yourHeaderFileVariable = [noOfItemsInArray count]; 
    NSLog(@"total item in table %d",yourHeaderFileVariable); 
    return [noOfItemsInArray count]; 
} 

或試試這個

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section  { 

    NSLog(@"total item in table %d",[noOfItemsInArray count]); 
    return [noOfItemsInArray count]; 
} 
+0

@Rahul Vyas:嗨,謝謝,但是如何獲取NSInteger變量中的變量noOfItemsInArray。 – sujay 2011-04-27 11:56:03

+0

簡單的NSInteger count = [yourArray count]; //注意yourArray指的是NSArray/NSMutableArray的一個對象。如果你調用[yourArray count]方法,這個方法將返回一個NSInteger,如果你想看到這個方法返回請參閱NSArray/NSMutableArray的文檔 – 2011-04-27 11:58:53

+0

@sameer請提高我的答案,如果它可以幫助你。 – 2011-04-27 12:00:03

-1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 



     return numberOfRows; 
    } 
+0

我想要一個變量中的數字,以便我可以在控制檯上打印 – sujay 2011-04-27 11:39:47

+0

int rowCount =「任何你想要的」 – visakh7 2011-04-27 11:44:16

+0

你的意思是NSInteger * rowCount =「數組列表」 – sujay 2011-04-27 11:47:20

-1

此功能在你的tableview

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [listOfItems count]; 
    } 
0

返回的行數(項目)在節表視圖中的行數是你從(NSInteger)tableView:numberOfRowsInSection:返回什麼。所以如果你從這個方法返回類似[mySource count]的東西,那麼行數是[mySource count]

例如:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [data count]; 
} 

// to get the count 
NSInteger rowCount = [data count]; 

現在更換[data count]你所擁有的委託方法計算什麼。

0
- (NSInteger)tableView:(UITableView *)tablefirst numberOfRowsInSection:(NSInteger)section { 

int count; 
    count=0; 
    count=[*your array name* count]; 
    NSLog(@"row count=%d",count); 
return [*your array name* count]; 
    } 
+0

如何獲得一個變量的計數,以便我可以在控制檯上打印 – sujay 2011-04-27 11:43:08

0

您需要實現的委託方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
return [yourArray count]; 
} 

然後,您可以通過以下的委託方法顯示在表頭的結果:

- (NSString *)tableView:(UITableView *)resultTableView titleForHeaderInSection:(NSInteger)section 
{ 
    NSString *str=[NSString stringWithFormat:@"%i",[yourArray count]]; 
    str=[str stringByAppendingString:@" matches"]; 
    return str; 
} 

For例如,如果表中有7個結果,標題將顯示文本「7個匹配項」