一个ViewController中的多个UITableView

时间:2015-06-02 16:26:47

标签: ios objective-c uitableview

为什么不在其他之后触发?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.tabFourContacts)
{
    static NSString *CellIdentifier = @"ContactCell";
    ContactCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if ([self.contactsArray[indexPath.row] hasPrefix:@"tel_"]){
        cell.labelContact.text = [self.contactsArray[indexPath.row] stringByReplacingOccurrencesOfString:@"tel_" withString:@""];
        cell.iconContact.image = [UIImage imageNamed:@"ic_tab4_contacts_phone.png"];
    }

    ...

    if ([self.contactsArray[indexPath.row] hasPrefix:@"addr_"]){
        cell.labelContact.text = [self.contactsArray[indexPath.row] stringByReplacingOccurrencesOfString:@"addr_" withString:@""];
        cell.iconContact.image = [UIImage imageNamed:@"ic_tab4_contacts_address.png"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}
else
{
    static NSString *CellIdentifier = @"FeedbackCell";
    FeedbackCell * cellFeedback = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    Feedback * feedbackObject;
    feedbackObject = [feedbacksArray objectAtIndex:indexPath.row];

    cellFeedback.feedNameLabel.text = feedbackObject.name;
    cellFeedback.feedDateLabel.text = feedbackObject.date;
    cellFeedback.feedTextLabel.text = feedbackObject.feedback;

    if ([feedbackObject.isGood isEqualToString:@"1"])
    {
        cellFeedback.feedImageView.image = [UIImage imageNamed:@"ic_ratingup_green.png"];
    }
    if ([feedbackObject.isGood isEqualToString:@"0"])
    {
        cellFeedback.feedImageView.image = [UIImage imageNamed:@"ic_ratingdown_red.png"];
    }
    return cellFeedback;
}
}

如果第二种情况指定ifelse if (tableView == self.tabFiveFeedbacks),则该方法请求返回单元格;它明确地说明了......

添加NSLog以检查numberOfRowsInSection:的两个tableview:

2015-06-02 22:41:08.052 MyApp[27994:1093141] numRowsForTab1: 0
2015-06-02 22:41:08.052 MyApp[27994:1093141] numRowsForTab1: 0
2015-06-02 22:41:08.053 MyApp[27994:1093141] numRowsForTab2: 0
2015-06-02 22:41:08.053 MyApp[27994:1093141] numRowsForTab2: 0
2015-06-02 22:41:08.061 MyApp[27994:1093141] numRowsForTab2: 0
2015-06-02 22:41:08.061 MyApp[27994:1093141] numRowsForTab1: 0
2015-06-02 22:41:08.217 MyApp[27994:1093141] numRowsForTab1: 1

NSLog为每个部分调用3-4次是否正确?

1 个答案:

答案 0 :(得分:0)

如果你这样做,你应该这样做。并与其他代表一起做山姆。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtindexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = nil;
    if (tableView == self.tableView1) {
        //configure the cell for tableView1
        cell = [tableView dequeueReusableCellWithIdentifier:@"identifierForCell1" forIndexPath:indexPath];
        cell.textLabel.text = @"text for cell tableView 1"
    } else if (tableView == self.tableView2) {
        //configure the cell for tableView2
        cell = [tableView dequeueReusableCellWithIdentifier:@"identifierForCell2" forIndexPath:indexPath];
        cell.textLabel.text = @"text for cell tableView 2"
    } else {
        //configure the cell for any other tableView
        //the cell object will never be nil,
        //it will always fall back to here.
        cell = [tableView dequeueReusableCellWithIdentifier:@"identifierForAnyOtherCell" forIndexPath:indexPath];
        cell.textLabel.text = @"text for any other cell"
    }
    return cell;
}

self.tableView1替换为您自己的tableView(如果需要,还替换tableView2)

请注意,您应将所有UITableView的委托设置为此UIViewController

self.tableView1.delegate = self;
self.tableView1.dataSource = self;
self.tableView2.delegate = self;
self.tableView2.dataSource = self;