iOS:在 UITableview 中以编程方式创建的按钮重复两次?

标签 ios iphone objective-c uitableview

我的项目基于解析 XML 数据和显示屏幕,在我的项目中,我需要在表格 View 单元格内编程设置 UIButton。我做到了,但是当我运行它时,按钮会重复两次。像这样
enter image description here

我不知道要解决这个问题这是我尝试过的代码

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0)
        return  [eventarray count];
    if (section == 1)
        return 1;
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"eventCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

if(indexPath.section == 0)
    {
        Eventlist *msglist = [eventarray objectAtIndex:indexPath.row];
        cell.textLabel.text = msglist.invitationdet;
        NSLog(@"Array %@",[SingleTonClass sinlgeTon].colorArray);
        NSInteger stat=msglist.readflag;
        if([[SingleTonClass sinlgeTon].colorArray containsObject:[NSString stringWithFormat:@"%d",indexPath.row]] || stat == 1 ) {
            NSInteger stat1 = msglist.responseflag;
            if(stat1 == 1){
                cell.textLabel.textColor = [UIColor yellowColor];
            }
            else {
             cell.textLabel.textColor = [UIColor redColor];
            }
        }
               else{
        cell.textLabel.textColor = [UIColor greenColor];
        }
        cell.backgroundColor = [UIColor blackColor];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

          }
    if(indexPath.section == 1)
    {
        UIButton *viewmoreButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        viewmoreButton.frame = CGRectMake(200.0f, 5.0f, 80.0f, 30.0f);
        [viewmoreButton setTitle:@"View More" forState:UIControlStateNormal]; 
        [cell addSubview:viewmoreButton];
        [viewmoreButton addTarget:self
                           action:@selector(viewMore:)
                 forControlEvents:UIControlEventTouchUpInside];
        cell.backgroundColor = [ UIColor blackColor];
    }
    return cell;
}

该按钮必须仅显示在第二个单元格上,请帮助我如何解决此问题 提前致谢

最佳答案

我认为问题在于您将按钮添加到 UITableViewCell ,然后当 reloadData在 table 上调用时,该单元格将排队等待重用,而该按钮仍附加在该按钮上。

创建您自己的UITableViewCell子类并将其用于需要自定义按钮的表格部分。

关于iOS:在 UITableview 中以编程方式创建的按钮重复两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22188459/

相关文章:

ios在viewDidLoad中获取 Storyboard名称

ios - unregisterForRemoteNotifications 不适用于 iOS8 - 推送通知

ios - 是否可以在没有任何互联网连接/禁用蜂窝网络的情况下使用核心定位/GPS?

ios - 如何以编程方式对 UILabel 应用约束,使其位于 Objective C 中 View 底部上方 20px

iphone - Quartz 2D/OpenGLES 图像上的几何变形(最好使用 CGImage)

iOS 推送通知不播放声音且不显示角标(Badge)警报 [在 Objective C 中]

ios - UICollectionView 右侧高度单元格重叠

ios - 以编程方式配置 iPad IP 地址

iphone - 如何在 UITableView beginUpdates/endUpdates 上检测到动画已经结束?

iphone - 如何在NavigationController中推送WebView