繁体   English   中英

在UITableView中的UITableViewCells之间切换

[英]Changing between UITableViewCells in UITableView

我是UITableViews的新手,对此主题我不太了解。

我在同一视图中有两个按钮,但在表中没有,用于控制UITableViewCells之间的切换,当按下一个按钮时,然后设置一个布尔值以加载特定的单元格。

  • 如果单击按钮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;
}

并为按钮设置ibaction,以将变量/ int buttonNumberClicked更改为1或2,具体取决于所按下的按钮和[tableView reloadData]; 以及内部练习,希望这会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM