简体   繁体   中英

tableView adds extra (empty) cell

I'm developing an iOS application which shows sessions in a UITableView. I'm currently in the phase of developing a custom tableviewcell to represent my data.

Based on the event type I'm changing the background color of a small bar in my tableviewcell (blue or orange). For some reason, the coloring is off and the tableview adds another cell which shouldn't be added. See image below.

在此处输入图片说明

Notice the first cell.. it has type: "ISKE" so should be orange, this is correct. Notice the second cell having the type: "ISKA". Now the bar on the right should've the color blue, which it is clearly not. It's weird though, because the font color of the date is in the correct color. Also notice the last bar being displayed, while there is no extra cell available.

I have no clue why this is happening. I hope someone can point me into the right direction. My get cell method is as followed:

public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
    Session session = ObjectModel.Current.AllSessions.ElementAt (indexPath.Row);

    SessionTableViewCell cell = tableView.DequeueReusableCell (session.SessionEvent.EventTitle) as SessionTableViewCell;

    if (cell == null) 
    {
        cell = new SessionTableViewCell ();
        var views = NSBundle.MainBundle.LoadNib ("SessionTableViewCell", cell, null);
        cell = Runtime.GetNSObject (views.ValueAt (0)) as SessionTableViewCell;
    }

    if (session.SessionEvent.EventTitle == "ISKE") {
        cell.SessionEventColor = UIColor.FromRGB (255, 165, 0);
        cell.SessionDateFontColor = UIColor.FromRGB (255, 165, 0);
    } else {
        cell.SessionEventColor = UIColor.FromRGB (80, 124, 191);
        cell.SessionDateFontColor = UIColor.FromRGB (80, 124, 191);
    }

    cell.SessionDay = session.SessionDate.DayOfWeek.ToString ().ToUpper ();
    cell.SessionTime = session.SessionDate.ToString ("HH:mm");
    cell.SessionDate = session.SessionDate.Day.ToString ().ToUpper ();
    cell.SessionSpeaker = "testspeaker";
    cell.ImgBgView.Layer.BorderColor = UIColor.FromRGB (206, 206, 208).CGColor;
    cell.ImgBgView.Layer.BorderWidth = 1.0f;

    cell.SelectionStyle = UITableViewCellSelectionStyle.None;
    cell.SessionTitle = session.SessionTitle;
    cell.SessionEvent = session.SessionEvent.EventTitle;
    cell.SessionRoom = session.SessionRoom;

    return cell;
}

检查您的数据源, UITableView高度和指定的单元格高度。

cell = new SessionTableViewCell ();
var views = NSBundle.MainBundle.LoadNib ("SessionTableViewCell", cell, null);
cell = Runtime.GetNSObject (views.ValueAt (0)) as SessionTableViewCell;

You don't have to call the constructor AND LoadNib . LoadNib should be enough. Calling both could lead to troubles. So your code should be:

var nibs = NSBundle.MainBundle.LoadNib ("SessionTableViewCell", cell, null);
cell = Runtime.GetNSObject (nibs.ValueAt (0)) as SessionTableViewCell;

your LoadNib usage looks wrong:

var views = NSBundle.MainBundle.LoadNib ("SessionTableViewCell", cell, null);

that means the owner is cell on it's own. I'd do

var views = NSBundle.MainBundle.LoadNib ("SessionTableViewCell", this, null);

hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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