我是 UITableViews 的新手,我对这个话题不是很了解。
我在同一个 View 中有两个按钮,但不在表中,用于控制 UITableViewCells 之间的切换,当按下一个按钮时,然后设置一个 bool 值以加载特定的单元格。
- 如果您点击按钮 1,它会显示带有 CellType1 的表格。
- 如果您点击按钮 2,它会显示带有 CellType2 的表格。
我的目标是为此使用同一张表。但我不知道从哪里开始。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
if (clickButton1 == true)
{
clickButton1 *cell = (clickButton1*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CellType1" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.name.text = @"Trial";
return cell;
}
else if (clickButton2 == true)
{
clickButton2 *cell = (clickButton2*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CellType2" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
//cell.name.text = @"Trial";
return cell;
} else
我认为这段代码不是最好的。但是我现在迷路了。 提前致谢!
最佳答案
使用这样的教程示例...
//3</pre>
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.tweetsArray count];
}
//4
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//5
static NSString *cellIdentifier = @"SettingsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//6
NSString *tweet = [self.tweetsArray objectAtIndex:indexPath.row];
//7
[cell.textLabel setText:tweet];
[cell.detailTextLabel setText:@"via Codigator"];
return cell;
}
如果您有两个数组,例如 self.tweetArray1 和 self.tweetArray2,您可以将此代码更改为
//3</pre>
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
int i = buttonNumberClicked;
return [self.tweetsArray[i] count];
}
//4
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
//5
int i = buttonNumberClicked;
static NSString *cellIdentifier = @"SettingsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//6
NSString *tweet = [self.tweetsArray[i] objectAtIndex:indexPath.row];
//7
[cell.textLabel setText:tweet];
[cell.detailTextLabel setText:@"via Codigator"];
return cell;
}
并为您的按钮设置 ibactions,以根据按下的按钮和 [tableView reloadData] 将变量/int buttonNumberClicked 更改为 1 或 2;也在 ibaction 内部,希望这对您有所帮助。
关于ios - 在 UITableView 中的 UITableViewCells 之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17285159/