Ios 如何自定义cellForRowAtIndexPath TableView方法以根据所采取的操作显示来自不同来源的数据?
我基本上有两个数组,我希望在TableView中一次显示一个:Ios 如何自定义cellForRowAtIndexPath TableView方法以根据所采取的操作显示来自不同来源的数据?,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我基本上有两个数组,我希望在TableView中一次显示一个: @property (nonatomic, strong) NSArray *items; @property (nonatomic, strong) NSArray *historicItems; 也就是说,根据所采取的操作,项目或历史记录应显示在表视图中 选择要显示的阵列的正确方法是什么?要在TableView中显示的单元格在CellForRowatineXpath:indexPath方法中准备 在你的课堂上添加一个BOOL,
@property (nonatomic, strong) NSArray *items;
@property (nonatomic, strong) NSArray *historicItems;
也就是说,根据所采取的操作,项目或历史记录应显示在表视图中
选择要显示的阵列的正确方法是什么?要在TableView中显示的单元格在CellForRowatineXpath:indexPath方法中准备 在你的课堂上添加一个BOOL,并在你的行为中设置为真或假
//define in your .h or wherever you want to declare
BOOL flag;
而不是在你的行为中把它变成真的或假的。而不是在CellForRowAtIndexPath中,根据您的操作选择数据源
If (flag){
cell.textLabel.text=[items objectAtIndex:IndexPath.row];
}
else{
cell.textLabel.text=[historicalItems objectAtIndexPath.row];
}
您还必须使用相同的if-else语句从这两个数据源以及numberofcell函数中返回单元格计数。您可以添加bool变量并根据所采取的操作对其进行更改。在表视图中,数据源方法根据bool变量进行操作,例如:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (action == YES) //your first action
return items.count;
else //second action
return historicItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Your code to init cell here
.......
if (action == YES) //your first action
{
cell.textLabel.text = items[indexPath.row];
}
else //second action
{
cell.textLabel.text = historicItems[indexPath.row];
}
return cell;
}
希望这有助于这样做:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (showItems)
{
return items.count;
}
else
{
return historicItems.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
if (showItems)
{
cell.textLabel.text = [items objectAtIndex:indexPath.row];
}
else
{
cell.textLabel.text = [historicItems objectAtIndex:indexPath.row];
}
return cell;
}
第一路
如果两个数组的包含内容相同,则采用datasourcearray。然后按按钮操作将适当的数组数据复制到datasourcearray中
datasourcearray = [self.items copy];
[yourtable reloadData];
对另一个数组执行类似操作
第二条路
如果两个数组包含的内容不同,则采用一个布尔值,假设为isFirstSenarioFlag
按下按钮时切换值“是”或“否”。检查每个数据源和委托方法中的标志值,并在重新加载数据方法后加载表的相应数据
在第一种方式中,您不需要在每个表datasource和委托方法中检查datasource的设置标志值aur。此外,切换每种程序方法将降低运行时的复杂性,并易于管理内存 根据您可以选择的操作,重新加载包含所需项目的tableview。使用任意布尔变量跟踪选择的操作
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger count;
if (isItems == YES) //your first action
count = items.count;
else {
count = historicItems.count;
}
return count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = dequeueReusableCellWithIdentifier:@"Cell"];
if (isItems == YES)
{
cell.textLabel.text = items[indexPath.row];
}
else
{
cell.textLabel.text = historicItems[indexPath.row];
}
return cell;
}
不。我的意思是数据源中有两个数组。我想让表视图显示一个或另一个数组。了解我的问题被否决的原因会很有趣。